关于android百度地图定位的问题
package com.example.testmap;import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
public class MapMainActivity extends Activity {
// 定位相关
LocationClient mLocClient;
LocationData locData = null;
public MyLocationListenner myListener = new MyLocationListenner();
//定位图层
locationOverlay myLocationOverlay = null;
//MapView 是地图主控件
private MapView mMapView = null;
//用MapController完成地图控制
private MapController mMapController = null;
//MKMapViewListener 用于处理地图事件回调
MKMapViewListener mMapListener = null;
//UI相关
Button requestLocButton = null;
boolean isRequest = false;//是否手动触发请求定位
boolean isFirstLoc = true;//是否首次定位
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_main);
setTitle("定位功能");
requestLocButton = (Button)findViewById(R.id.button1);
requestLocButton.setOnClickListener(btnClickListener);
//地图初始化
mMapView = (MapView)findViewById(R.id.bmapView);
mMapController = mMapView.getController(); //获取地图控制器
mMapController.setZoom(13);//设置地图缩放级别;
mMapController.enableClick(true);//设置地图是否响应点击事件 .
mMapView.setBuiltInZoomControls(true);//显示内置缩放控件
//定位初始化
mLocClient = new LocationClient(this);
locData = new LocationData();
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);//打开gps
option.setCoorType("bd09ll");//设置坐标类型
option.setScanSpan(5000);
mLocClient.setLocOption(option);
mLocClient.start();
//定位图层初始化
myLocationOverlay = new locationOverlay(mMapView);
//设置定位数据
myLocationOverlay.setData(locData);
//添加定位图层
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
//修改定位数据后刷新图层生效
mMapView.refresh();
}
//定位按钮监听
OnClickListener btnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//手动定位请求
requestLocClick();
}
};
//定位SDK监听函数
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null){
return ;
}
locData.latitude = location.getLatitude();
locData.longitude = location.getLongitude();
//如果不显示定位精度圈,将accuracy赋值为0即可
locData.accuracy = location.getRadius();
locData.direction = location.getDerect();
//更新定位数据
myLocationOverlay.setData(locData);
//更新图层数据执行刷新后生效
mMapView.refresh();
//是手动触发请求或首次定位时,移动到定位点
if (isRequest || isFirstLoc){
//移动地图到定位点
mMapController.animateTo(new GeoPoint((int)(locData.latitude* 1e6), (int)(locData.longitude * 1e6)));
isRequest = false;
}
//首次定位完成
isFirstLoc = false;
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null){
return ;
}
}
}
//继承MyLocationOverlay重写dispatchTap实现点击处理
public class locationOverlay extends MyLocationOverlay{
public locationOverlay(MapView mapView) {
super(mapView);
// TODO Auto-generated constructor stub
}
}
//手动触发一次定位请求
public void requestLocClick(){
isRequest = true;
mLocClient.requestLocation();
Toast.makeText(MapMainActivity.this, "正在定位……", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
if (mLocClient != null){
mLocClient.stop();
}
mMapView.destroy();
super.onDestroy();
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
}
}
无法进入
//定位SDK监听函数
public class MyLocationListenner implements BDLocationListener {
是什么问题
android 百度地图 gps --------------------编程问答-------------------- 初始化地图引擎没有啊?
而且初始化地图引擎的代码要放在setContentView(R.layout.activity_map_main);的前面 --------------------编程问答--------------------
不是 已经解决了 --------------------编程问答-------------------- 请问是怎么解决的,我也碰到了同样 的问题 --------------------编程问答--------------------
有个打开权限没加 --------------------编程问答-------------------- 用GPS定位好慢,基站那个定位的挺快的 --------------------编程问答-------------------- 看看 API可以吗
--------------------编程问答-------------------- 对 应该是权限问题 呵呵 --------------------编程问答-------------------- 哦,无法定位终于找到问题所在了,谢谢楼主,是有易做图限没加,没加
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service> --------------------编程问答-------------------- 8楼 谢谢了,我也查了半天不知道什么原因
补充:移动开发 , Android