新版百度地图的定位问题
public class MainActivity extends Activity {
private MapView mapView = null;
private BMapManager bMapManager = null;
private String Key = "1d4b16d300bdc20e68643c9050edaf72";
private MapController mapController = null;
private MyLocationOverlay mLocationOverlay;
private LocationClient lClient;
private LocationData lData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initMapManager();
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true); //设置mapview可控
mapController = mapView.getController(); //获得mapview的控制对象
//设置一个经纬度
// GeoPoint geoPoint = new GeoPoint((int)(39.945*1E6), (int)(116.404*1E6));
//
// mapController.setCenter(geoPoint);
mapController.setZoom(12);
mapController.enableClick(true);
//设置地图样式:交通图/卫星图
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch (arg1) {
case R.id.radiobut1:
mapView.setTraffic(true);
mapView.setSatellite(false);
break;
case R.id.radiobut2:
mapView.setSatellite(true);
mapView.setTraffic(false);
break;
}
}
});
lClient = new LocationClient(this);
lData = new LocationData();
lClient.registerLocationListener(bdListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setCoorType("bd09ll");
option.setScanSpan(1000);
lClient.setLocOption(option);
lClient.start();
mLocationOverlay = new MyLocationOverlay(mapView);
mLocationOverlay.setData(lData);
mLocationOverlay.setLocationMode(LocationMode.NORMAL);
mapView.getOverlays().add(mLocationOverlay);
mLocationOverlay.enableCompass();
mapView.refresh();
}
private BDLocationListener bdListener = new BDLocationListener() {
@Override
public void onReceivePoi(BDLocation location) {
if(location == null)
return ;
//获得经纬度
lData.latitude = location.getLatitude();
lData.longitude = location.getLongitude();
//如果不显示定位精度圈,将accuracy赋值为0即可
lData.accuracy = location.getRadius();
// 此处可以设置 locData的方向信息, 如果定位 SDK 未返回方向信息,用户可以自己实现罗盘功能添加方向信息。
lData.direction = location.getDerect();
mLocationOverlay.setData(lData);
mapView.refresh();
}
@Override
public void onReceiveLocation(BDLocation poiLocation) {
// TODO Auto-generated method stub
if(poiLocation == null)
return;
}
};
private void initMapManager() {
bMapManager = new BMapManager(MainActivity.this);
bMapManager.init(Key, new MKGeneralListener() {
@Override
public void onGetPermissionState(int arg0) {
if(arg0 == 300){
Toast.makeText(MainActivity.this, "输入的Key值有错!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onGetNetworkState(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
protected void onDestroy() {
if(mapView != null){
mapView.destroy();
mapView = null;
}
if(lClient != null){
lClient.stop();
}
super.onDestroy();
}
@Override
protected void onPause() {
if(mapView != null)
mapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
if(mapView != null)
mapView.onResume();
super.onResume();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mapView.onRestoreInstanceState(savedInstanceState);
}
}
看了很多资料,都是以前老版本的百度sdk,继承的还是MapActivity。我这个是仿照官网的demo写的百度地图定位,但是进去之后没有定位,只在地图初始化的北京那个位置。求高手帮我看看哪里出问题了,谢谢。在线等。 --------------------编程问答-------------------- 新版不是把定位剥离出一个单独的定位SDK了么? --------------------编程问答--------------------
是啊,BDLocationListener 这个类就是新版的sdk里面引用的。 --------------------编程问答-------------------- 我贴一下权限和服务吧
<?xml version="1.0" encoding="utf-8"?>--------------------编程问答-------------------- 应该是重写onReceiveLocation,你写在了onReceivePoi里面。你写错了
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zonten.baiduditu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:debuggable="true"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name="com.zonten.baiduditu.MainActivity"
android:label="@string/app_name"
android:screenOrientation="sensor"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote">
</service>
</application>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</permission>
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
</uses-permission>
<!-- <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? -->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CAMERA" >
</uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 来电消音 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="false"
android:resizeable="true"
android:smallScreens="true" />
</manifest>
然后在onReceiveLocation加上
objpoint=new GeoPoint((int)(locData.latitude* 1e6), (int)(locData.longitude * 1e6));
mapController.setCenter(objpoint); --------------------编程问答-------------------- http://blog.csdn.net/xiaanming/article/details/11380619看看这个吧,希望对你有帮助
补充:移动开发 , Android