|
|
@@ -1,12 +1,12 @@
|
|
|
package com.xzjmyk.pm.activity.util;
|
|
|
|
|
|
-import android.os.Bundle;
|
|
|
-import android.os.Handler;
|
|
|
-import android.os.Message;
|
|
|
-import android.util.Log;
|
|
|
+import android.content.Context;
|
|
|
|
|
|
import com.baidu.location.BDLocation;
|
|
|
import com.baidu.location.BDLocationListener;
|
|
|
+import com.baidu.location.LocationClient;
|
|
|
+import com.baidu.location.LocationClientOption;
|
|
|
+import com.baidu.location.Poi;
|
|
|
import com.baidu.mapapi.map.BitmapDescriptor;
|
|
|
import com.baidu.mapapi.map.BitmapDescriptorFactory;
|
|
|
import com.baidu.mapapi.map.MapStatus;
|
|
|
@@ -28,17 +28,17 @@ import com.baidu.mapapi.utils.DistanceUtil;
|
|
|
import com.xzjmyk.pm.activity.R;
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
+import java.util.Hashtable;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 百度地图工具类(重复代码过多,放在工具类中)
|
|
|
* Created by gongpm on 2016/7/15.
|
|
|
*/
|
|
|
-public class BaiduMapUtil implements OnGetPoiSearchResultListener {
|
|
|
+public class BaiduMapUtil {
|
|
|
private static BaiduMapUtil instence = null;
|
|
|
- private Handler handler;
|
|
|
private PoiNearbySearchOption option;
|
|
|
- public static int NEAR_WHAT = 0x22;
|
|
|
- private int what = -1;
|
|
|
|
|
|
private BaiduMapUtil() {
|
|
|
}
|
|
|
@@ -52,6 +52,12 @@ public class BaiduMapUtil implements OnGetPoiSearchResultListener {
|
|
|
return instence;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 设置当前地图显示位置
|
|
|
+ *
|
|
|
+ * @param mapView mapView视图控件
|
|
|
+ * @param point 显示位置
|
|
|
+ */
|
|
|
public void setMapViewPoint(MapView mapView, LatLng point) {
|
|
|
setMapViewPoint(mapView, point, false);
|
|
|
}
|
|
|
@@ -75,7 +81,13 @@ public class BaiduMapUtil implements OnGetPoiSearchResultListener {
|
|
|
return getDistance(a, b);
|
|
|
}
|
|
|
|
|
|
- //显示当前位置
|
|
|
+ /**
|
|
|
+ * 将定点显示在指定位置
|
|
|
+ *
|
|
|
+ * @param mapView 地图控件
|
|
|
+ * @param point 显示位置
|
|
|
+ * @param isClear 是否清除前面的点
|
|
|
+ */
|
|
|
public void setMapViewPoint(MapView mapView, LatLng point, boolean isClear) {
|
|
|
// 构建Marker图标
|
|
|
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
|
|
|
@@ -90,10 +102,16 @@ public class BaiduMapUtil implements OnGetPoiSearchResultListener {
|
|
|
mapView.getMap().animateMapStatus(u);//设置为中心显示
|
|
|
}
|
|
|
|
|
|
- //监听周围信息的获取 radius=0 为默认显示周围1000米
|
|
|
- public void getNearInfo(int radius, int pagerNum, LatLng location, PoiSearch mPoiSearch, Handler handler, int what) {
|
|
|
- this.handler = handler;
|
|
|
- this.what = what;
|
|
|
+ /**
|
|
|
+ * 监听周围信息的获取 radius=0 为默认显示周围1000米
|
|
|
+ *
|
|
|
+ * @param radius 范围
|
|
|
+ * @param pagerNum 页数
|
|
|
+ * @param location 当前(中心)位置
|
|
|
+ * @param mPoiSearch 必须类API
|
|
|
+ * @param neerPoiListener 接口回调
|
|
|
+ */
|
|
|
+ public void getNearInfo(int radius, int pagerNum, LatLng location, PoiSearch mPoiSearch, final OnNeerPoiListener neerPoiListener) {
|
|
|
if (option == null)
|
|
|
option = new PoiNearbySearchOption();
|
|
|
option.keyword("写字楼");
|
|
|
@@ -110,84 +128,125 @@ public class BaiduMapUtil implements OnGetPoiSearchResultListener {
|
|
|
option.pageNum(1);
|
|
|
}
|
|
|
option.pageCapacity(20);
|
|
|
- mPoiSearch.setOnGetPoiSearchResultListener(this);
|
|
|
- mPoiSearch.searchNearby(option);
|
|
|
- }
|
|
|
+ mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
|
|
|
+ @Override
|
|
|
+ public void onGetPoiResult(PoiResult poiResult) {
|
|
|
+ if (neerPoiListener == null) return;
|
|
|
+ neerPoiListener.result(poiResult);
|
|
|
+ }
|
|
|
|
|
|
- public void getLocation() {
|
|
|
+ @Override
|
|
|
+ public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
|
|
|
+ if (neerPoiListener == null) return;
|
|
|
+ neerPoiListener.result(poiDetailResult);
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
|
|
|
+ if (neerPoiListener == null) return;
|
|
|
+ neerPoiListener.result(poiIndoorResult);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mPoiSearch.searchNearby(option);
|
|
|
}
|
|
|
|
|
|
- public void getNearInfo(int radius, int pagerNum, LatLng location, Handler handler) {
|
|
|
- getNearInfo(radius, pagerNum, location, PoiSearch.newInstance(), handler, NEAR_WHAT);
|
|
|
+ /**
|
|
|
+ * 获取周围位置信息
|
|
|
+ *
|
|
|
+ * @param radius 范围
|
|
|
+ * @param pagerNum 页数
|
|
|
+ * @param location 中心地图
|
|
|
+ * @param neerPoiListener 接口回调
|
|
|
+ */
|
|
|
+ public void getNearInfo(int radius, int pagerNum, LatLng location, OnNeerPoiListener neerPoiListener) {
|
|
|
+ getNearInfo(radius, pagerNum, location, PoiSearch.newInstance(), neerPoiListener);
|
|
|
}
|
|
|
|
|
|
- private void setDataToHandler(SearchResult result) {
|
|
|
- if (handler == null) return;
|
|
|
- Message message = handler.obtainMessage();
|
|
|
- message.what = this.what;
|
|
|
- Bundle bundle = new Bundle();
|
|
|
- bundle.putParcelable("data", result);
|
|
|
- message.setData(bundle);
|
|
|
- handler.sendMessage(message);
|
|
|
+ //获取周围位置信息接口
|
|
|
+ public interface OnNeerPoiListener {
|
|
|
+ void result(SearchResult result);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void onGetPoiResult(PoiResult poiResult) {
|
|
|
- if (handler == null) return;
|
|
|
- Message message = handler.obtainMessage();
|
|
|
- message.what = this.what;
|
|
|
- Bundle bundle = new Bundle();
|
|
|
- bundle.putParcelable("data", poiResult);
|
|
|
- message.setData(bundle);
|
|
|
- handler.sendMessage(message);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 结束获取周围位置信息
|
|
|
+ * <p/>
|
|
|
+ * <p/>
|
|
|
+ * <p/>
|
|
|
+ * <p/>
|
|
|
+ * 开始定位功能
|
|
|
+ */
|
|
|
|
|
|
- @Override
|
|
|
- public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
|
|
|
- setDataToHandler(poiDetailResult);
|
|
|
+ private static LocationClient mLocationClient = null;//使用过多,定义静态避免不必要新建
|
|
|
+ private OnLocationListener locationListener;
|
|
|
+
|
|
|
+ public void getPoiInfo(Context context, OnLocationListener locationListener) {
|
|
|
+ this.locationListener = locationListener;
|
|
|
+ if (mLocationClient == null)
|
|
|
+ mLocationClient = new LocationClient(context); // 声明LocationClient类
|
|
|
+ mLocationClient.registerLocationListener(bdLocationListener); // 注册监听函数
|
|
|
+ initLocation();
|
|
|
+ if (!mLocationClient.isStarted()) {
|
|
|
+ mLocationClient.start();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
|
|
|
- setDataToHandler(poiIndoorResult);
|
|
|
+ private void initLocation() {
|
|
|
+ LocationClientOption option = new LocationClientOption();
|
|
|
+ option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
|
|
|
+ );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
|
|
|
+ option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
|
|
|
+ int span = 0;
|
|
|
+ option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
|
|
|
+ option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
|
|
|
+ option.setOpenGps(true);//可选,默认false,设置是否使用gps
|
|
|
+ option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
|
|
|
+ option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
|
|
|
+ option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
|
|
|
+ option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
|
|
|
+ option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
|
|
|
+ option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
|
|
|
+ mLocationClient.setLocOption(option);
|
|
|
}
|
|
|
|
|
|
+ public interface OnLocationListener {
|
|
|
+ void result(Map<String, Object> map);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 实现实位回调监听
|
|
|
- */
|
|
|
- public class MyLocationListener implements BDLocationListener {
|
|
|
+ //参考获取定位数据
|
|
|
+ private BDLocationListener bdLocationListener = new BDLocationListener() {
|
|
|
+ @Override
|
|
|
public void onReceiveLocation(BDLocation location) {
|
|
|
- //Receive Location
|
|
|
- StringBuffer sb = new StringBuffer(256);
|
|
|
- sb.append("time : ");
|
|
|
- sb.append(location.getTime());//获得当前时间
|
|
|
- sb.append("\nerror code : ");
|
|
|
- sb.append(location.getLocType());//获得erro code得知定位现状
|
|
|
- sb.append("\nlatitude : ");
|
|
|
- sb.append(location.getLatitude());//获得纬度
|
|
|
- sb.append("\nlontitude : ");
|
|
|
- sb.append(location.getLongitude());//获得经度
|
|
|
- sb.append("\nradius : ");
|
|
|
- sb.append(location.getRadius());
|
|
|
- if (location.getLocType() == BDLocation.TypeGpsLocation) {//通过GPS定位
|
|
|
- sb.append("\nspeed : ");
|
|
|
- sb.append(location.getSpeed());//获得速度
|
|
|
- sb.append("\nsatellite : ");
|
|
|
- sb.append(location.getSatelliteNumber());
|
|
|
- sb.append("\ndirection : ");
|
|
|
- sb.append("\naddr : ");
|
|
|
- sb.append(location.getAddrStr());//获得当前地址
|
|
|
- sb.append(location.getDirection());//获得方位
|
|
|
- } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {//通过网络连接定位
|
|
|
- sb.append("\naddr : ");
|
|
|
- sb.append(location.getAddrStr());//获得当前地址
|
|
|
- //运营商信息
|
|
|
- sb.append("\noperationers : ");
|
|
|
- sb.append(location.getOperators());//获得经营商?
|
|
|
+ Map<String, Object> map = new Hashtable<>();
|
|
|
+ map.put("time", location.getTime());
|
|
|
+ map.put("code", location.getLocType());
|
|
|
+ map.put("latitude", location.getLatitude());
|
|
|
+ map.put("lontitude", location.getLongitude());
|
|
|
+ map.put("radius", location.getRadius());
|
|
|
+ if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
|
|
|
+ map.put("speed", location.getSpeed());// 单位:公里每小时
|
|
|
+ map.put("satellite", location.getSatelliteNumber());
|
|
|
+ map.put("height", location.getAltitude());// 单位:米
|
|
|
+ map.put("direction", location.getDirection());// 单位度
|
|
|
+ map.put("addr", location.getAddrStr());
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
|
|
|
+ map.put("addr", location.getAddrStr());
|
|
|
+ map.put("describe", "网络定位成功");
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
|
|
|
+ map.put("describe", "离线定位成功");
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeServerError) {
|
|
|
+ map.put("describe", "服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
|
|
|
+ map.put("describe", "网络不同导致定位失败,请检查网络是否通畅");
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
|
|
|
+ map.put("describe", "无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
|
|
|
+ } else {
|
|
|
+ map.put("describe", "未知原因错误");
|
|
|
}
|
|
|
- Log.i("BaiduLocationApiDem", sb.toString());
|
|
|
+ map.put("name", location.getLocationDescribe());// 位置语义化信息
|
|
|
+ List<Poi> list = location.getPoiList();// POI数据
|
|
|
+ map.put("poi", list.get(0).getName());//
|
|
|
+ if (locationListener == null) return;
|
|
|
+ locationListener.result(map);
|
|
|
}
|
|
|
- }
|
|
|
+ };
|
|
|
}
|