weixin_38709807
Re-creater
2017-09-01 02:52

百度地图sdk 开发定位功能 得到定位城市为空,怎么回事?

  • 百度
  • sdk

package com.maxh.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.maxh.data.City;

import java.util.List;

/**

  • Created by Administrator on 2017/8/31. */

public class LocateActivity extends Activity{
private LocationClient mLocationClient;
private MyLocationListener myLocationListener;
private LocationClientOption option;
Button cityBt;
private String mLocCityCode;
private List cityList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mLocationClient=new LocationClient(getApplicationContext());
    setContentView(R.layout.locate);
    cityBt= (Button) findViewById(R.id.city_bt);
    myLocationListener=new MyLocationListener(cityBt);
    mLocationClient.registerLocationListener(myLocationListener);
    initLocation();
    mLocationClient.start();
}

private void initLocation() {
    option=new LocationClientOption();
    option.setIsNeedAddress(true);
    option.setOpenGps(true);
    option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
    option.setCoorType("bd09ll");
    option.setScanSpan(1000);
    mLocationClient.setLocOption(option);
}

public class MyLocationListener implements BDLocationListener {
Button cityBt;
MyLocationListener(Button cityBt){
this.cityBt=cityBt;
}
String cityName;
@Override
public void onReceiveLocation(BDLocation bdLocation) {
cityName = bdLocation.getCity();
Log.d("Locate",cityName);
cityBt.setText(cityName);
}
}
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答