2 weixin 38709807 weixin_38709807 于 2017.09.01 10:52 提问

百度地图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);
}
}
}

2个回答

qq_26433213
qq_26433213   2017.09.01 11:07

手机有没有开启定位的权限,这个问题经常犯得

weixin_38709807
weixin_38709807 开了的,在AndroidManifest里配置了的
3 个月之前 回复
Naide_S
Naide_S   2017.09.02 09:17

登陆百度地图 检查一下你的appkey 配置正常 再看一下你的sha1的值是否正确(分调试版和正式版 如果debug可以 打包后不行 就是这个问题) 如果都没有问题 再根据百度文档在检查一下你的代码 问题不大 细心一点就可以解决

Csdn user default icon
上传中...
上传图片
插入图片