您的位置:首页 > 其它

百度地图 定位,地图中心坐标,拖动后获取地图中心点坐标,poi搜索

2017-06-14 14:48 465 查看
百度地图定位

声明成员变量

private boolean isFirstLoc = true;//首次

private boolean isRequest  = false;//手动请求

//ui

private MapView mMapView = null;

private ImageView img_pos;

private MapView mMapView = null;

private BaiduMap mBaiduMap;

public LocationClient mLocationClient = null;
public BDLocationListener myListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
if (location.getLocType() == BDLocation.TypeGpsLocation){
// GPS定位结果
locationSucess(location);
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
// 网络定位结果
locationSucess(location);
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
// 离线定位结果
locationSucess(location);
} else if (location.getLocType() == BDLocation.TypeServerError) {
ToastUtil.showToast(getApplicationContext(), "定位失败");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
ToastUtil.showToast(getApplicationContext(), "网络不通导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
ToastUtil.showToast(getApplicationContext(), "无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
}

private void locationSucess(BDLocation location) {
LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
if (isRequest || isFirstLoc){//isRequest = true的话,这里可以只判断 isRequest就可以,
isRequest = false;
ArrayList<LatLng> listLatlngs = new ArrayList<LatLng>();
listLatlngs.add(latlng);
goMarkerCenter(mBaiduMap, listLatlngs);
latlng2tv(latlng);
}
//首次定位完成
isFirstLoc = false;

}
@Override
public void onConnectHotSpotMessage(String arg0, int arg1) {
}
};

手动获取定位  

mLocationClient.requestLocation();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SDKInitializer.initialize(getApplicationContext());  

setContentView(R.layout.activity_position_update);

//获取地图控件引用
mMapView = (MapView) findViewById(R.id.bmapView); 

img_pos = (ImageView) findViewById(R.id.img_pos);

initBaiduMap(mMapView);

}

初始化地图

private void initBaiduMap(MapView mMapView) {
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
//普通地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
//定位********************
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true); // 不显示我的位置,样覆盖物代替
mBaiduMap.setMaxAndMinZoomLevel(18, 3); // 地图的最大最小缩放比例3-18
mBaiduMap.setOnMapStatusChangeListener(onMapStatusChangeListener);
// 隐藏logo
View child = mMapView.getChildAt(1);
if (child != null && (child instanceof ImageView || child instanceof ZoomControls)){            
child.setVisibility(View.INVISIBLE);           
}

         // 不显示地图上比例尺    

         mMapView.showScaleControl(false);    

         // 不显示地图缩放控件(按钮控制栏)    

         mMapView.showZoomControls(false);
//声明LocationClient类
mLocationClient = new LocationClient(getApplicationContext());
//注册监听函数
mLocationClient.registerLocationListener(myListener);
initLocation();
mLocationClient.start();
}

/*官方copy*/

private void initLocation(){

LocationClientOption option = new LocationClientOption();
//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setLocationMode(LocationMode.Hight_Accuracy);
//可选,默认gcj02,设置返回的定位结果坐标系
option.setCoorType("bd09ll");
int span=1000;
//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setScanSpan(0);
//可选,设置是否需要地址信息,默认不需要
option.setIsNeedAddress(true);
//可选,默认false,设置是否使用gps
option.setOpenGps(true);
//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
option.setLocationNotify(true);
//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationDescribe(true);
//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIsNe
b0cb
edLocationPoiList(true);
//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死  
option.setIgnoreKillProcess(false);
//可选,默认false,设置是否收集CRASH信息,默认收集
option.SetIgnoreCacheException(false);
//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
option.setEnableSimulateGps(false);

mLocationClient.setLocOption(option);
}

/**
* 将latLngs中的坐标  缩放进屏幕中
*/
public static void goMarkerCenter(BaiduMap mBaiduMap, ArrayList<LatLng> latLngs){
LatLngBounds.Builder builder = new LatLngBounds.Builder();
double minLat = latLngs.get(0).latitude;
double maxLat = latLngs.get(0).latitude;
double minLon = latLngs.get(0).longitude;
double maxLon = latLngs.get(0).longitude;
for(LatLng ll : latLngs){
if(ll!=null){
minLat = minLat < ll.latitude? minLat : ll.latitude;
maxLat = maxLat > ll.latitude? maxLat : ll.latitude;
minLon = minLon < ll.longitude?minLon : ll.longitude;
maxLon = maxLon > ll.longitude?maxLon : ll.longitude;
}
}
builder.include(new LatLng(minLat- 0.005,minLon-0.005));
builder.include(new LatLng(maxLat+0.005,maxLon+0.005));
LatLngBounds bounds = builder.build();

MapStatusUpdate u = MapStatusUpdateFactory
.newLatLngBounds(bounds);
mBaiduMap.setMapStatus(u);
}

/**为了实现 拖动地图后 ,获取屏幕中心的位置*/

OnMapStatusChangeListener onMapStatusChangeListener = new OnMapStatusChangeListener() {  
/** 
* 手势操作地图,设置地图状态等操作导致地图状态开始改变。 
* @param status 地图状态改变开始时的地图状态 
*/  
public void onMapStatusChangeStart(MapStatus status){  
ToastUtil.showToast(getApplicationContext(), "开始滑动");
}  
/** 
* 地图状态变化中 
* @param status 当前地图状态 
*/  
public void onMapStatusChange(MapStatus status){  
}  
/** 
* 地图状态改变结束 
* @param status 地图状态改变结束后的地图状态 
*/  
public void onMapStatusChangeFinish(MapStatus status){  
requestCenterPos(status);

}
};

/*获取地图中心位置

方法一 比 方法二 准确,可能是地图没有全屏吧?有知道的同学,请留言

*/

public void requestCenterPos(MapStatus status){

//方法一:

LatLng mCenterLatLng = status.target;

        /**获取经纬度*/

        double lat = mCenterLatLng.latitude;

        double lng = mCenterLatLng.longitude;

//方法二:

//获取屏幕点

//int[] location =new int[2];

//img_pos.getLocationOnScreen(location);  

//int x = location[0];

//int y = location[1];

//Point centerTop = new Point(x,y);

////获取屏幕点 的坐标位置

//LatLng latlng = mBaiduMap.getProjection().fromScreenLocation(centerTop);

}

/**地理编码步骤     里面的handler,是因为  包错,说是不能再子线程 更新 ui,监听器回调,应该都是在主线程吧?*/

/*获取该店 的poi*/

// //第一步,创建地理编码检索实例;

// if(mSearch == null){

// mSearch = GeoCoder.newInstance();

// }

// //第二步,创建地理编码检索监听者;

// OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {

// // 地理编码查询结果回调函数 

// public void onGetGeoCodeResult(GeoCodeResult result) {  

// if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {  

// //没有检索到结果  

// }  

// //获取地理编码结果  

// }  

// // 反地理编码查询结果回调函数 

// @Override  

// public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {  

// if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {  

// //没有找到检索结果  

// handler.post(new Runnable() {

// @Override

// public void run() {

//// ToastUtil.showToast(getApplicationContext(), "没有结果");

// }

// });

// }  else{

// //获取反向地理编码结果  

// List<PoiInfo> list = result.getPoiList();

// final StringBuffer sb = new StringBuffer();

// for (PoiInfo p : list) {

// sb.append("\npoi= : ");

// sb.append(p.address);

// }

// handler.post(new Runnable() {

// @Override

// public void run() {

// // TODO Auto-generated method stub

// ToastUtil.showToast(getApplicationContext(), sb.toString());

//

// }

// });

// }

//

// }  

// };

// //第三步,设置地理编码检索监听者;

// mSearch.setOnGetGeoCodeResultListener(listener);

// //第四步,发起地理编码检索;

// onDestroy()->销毁mSearch

// mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(latlng));//坐标->地址

//        mSearch.geocode(new GeoCodeOption().address("中国").city("西安"));//地址->坐标

//第五步,释放地理编码检索实例;

// mSearch.destroy();

配置性的东西 还是看官网文档,才放心
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐