|
|
@@ -11,6 +11,8 @@ import android.location.LocationListener;
|
|
|
import android.location.LocationManager;
|
|
|
import android.location.LocationProvider;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Looper;
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
@@ -18,6 +20,10 @@ import android.util.Log;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baidu.location.BDLocation;
|
|
|
+import com.baidu.location.BDLocationListener;
|
|
|
+import com.baidu.location.LocationClient;
|
|
|
+import com.baidu.location.LocationClientOption;
|
|
|
import com.baidu.mapapi.model.LatLng;
|
|
|
import com.me.network.app.http.HttpClient;
|
|
|
import com.me.network.app.http.Method;
|
|
|
@@ -40,6 +46,7 @@ import java.util.logging.Logger;
|
|
|
* 本地定位,只负责定位
|
|
|
*/
|
|
|
public class NativeLocationManager implements BaseLocationManager {
|
|
|
+ private final String DEF_OUT_CHS_COOE_TYPE = "WGS84";//bd09ll //WGS84
|
|
|
private final Logger MLOGGER = Logger.getLogger("NativeLocationManager");
|
|
|
|
|
|
private static final String TAG = "NativeLocationManager";
|
|
|
@@ -49,14 +56,48 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
private LocationManager mLocationManager;
|
|
|
private Context ct;
|
|
|
private HttpClient mHttpClient;
|
|
|
+ private LocationClient mLocationClient;
|
|
|
+ private Handler mMainHandler = null;
|
|
|
|
|
|
public NativeLocationManager(Context ct) {
|
|
|
if (ct != null) {
|
|
|
+ mMainHandler = new Handler(Looper.getMainLooper());
|
|
|
this.ct = ct.getApplicationContext();
|
|
|
mLocationManager = (LocationManager) ct.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
|
|
|
+ initBaiduManager();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 配置
|
|
|
+ * 百度地图SDK在国内(包括港澳台)使用的是BD09坐标(定位SDK默认使用GCJ02坐标);
|
|
|
+ * 在海外地区,统一使用WGS84坐标。开发者在使用百度地图相关服务时,请注意选择
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private LocationClientOption getOptionByGPS() {
|
|
|
+ LocationClientOption option = new LocationClientOption();
|
|
|
+ option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
|
|
|
+ option.setCoorType(DEF_OUT_CHS_COOE_TYPE);//可选,默认gcj02,设置返回的定位结果坐标系 bd09ll
|
|
|
+ option.setScanSpan(1);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
|
|
|
+ option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
|
|
|
+ option.setOpenGps(true);//可选,默认false,设置是否使用gps
|
|
|
+ option.setLocationNotify(false);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
|
|
|
+ option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
|
|
|
+ option.setIsNeedLocationPoiList(false);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
|
|
|
+ option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
|
|
|
+ option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
|
|
|
+ option.setEnableSimulateGps(true);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
|
|
|
+ option.setNeedDeviceDirect(false);
|
|
|
+ return option;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initBaiduManager() {
|
|
|
+ mLocationClient = new LocationClient(ct);
|
|
|
+ mLocationClient.registerLocationListener(mLocationListener); // 注册监听函数
|
|
|
+ mLocationClient.setLocOption(getOptionByGPS());
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 获取定位条件
|
|
|
@@ -82,30 +123,25 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
return provider;
|
|
|
}
|
|
|
|
|
|
- public void setLocation(Address address) {
|
|
|
+ public void setLocation(int type, String coord, Address address) {
|
|
|
if (mUASLocation == null) {
|
|
|
- mUASLocation = new UASLocation(UASLocation.TYPE_NATIVE);
|
|
|
- } else {
|
|
|
+ mUASLocation = new UASLocation(type);
|
|
|
+ } else if (type == mUASLocation.getType() || mUASLocation.getType() == UASLocation.TYPE_NATIVE || !mUASLocation.isLegalAble()) {
|
|
|
clearLocation();
|
|
|
+ mUASLocation.setType(type);
|
|
|
+ mUASLocation.setLocationOk(true);
|
|
|
+ mUASLocation.setProvince(address.getLocality());
|
|
|
+ mUASLocation.setCityName(address.getSubAdminArea());
|
|
|
+ mUASLocation.setCountry(address.getCountryName());
|
|
|
+ mUASLocation.setName(address.getFeatureName());
|
|
|
+ mUASLocation.setDistrict(address.getThoroughfare());
|
|
|
+ mUASLocation.setAddress(address.getAddressLine(0));
|
|
|
+ changeCoord(coord, new LatLng(address.getLatitude(), address.getLongitude()));
|
|
|
+ if (mOnLocationListener != null) {
|
|
|
+ mOnLocationListener.onReceiveLocation(mUASLocation);
|
|
|
+ }
|
|
|
+ release();
|
|
|
}
|
|
|
- mUASLocation.setLocationOk(true);
|
|
|
- mUASLocation.setProvince(address.getLocality());
|
|
|
- mUASLocation.setCityName(address.getSubAdminArea());
|
|
|
- mUASLocation.setCountry(address.getCountryName());
|
|
|
- mUASLocation.setName(address.getFeatureName());
|
|
|
- mUASLocation.setDistrict(address.getThoroughfare());
|
|
|
- mUASLocation.setAddress(address.getAddressLine(0));
|
|
|
- LatLng latlng = CoordinateUtils.common2Baidu(new LatLng(address.getLatitude(), address.getLongitude()));
|
|
|
-// latlng = CoordinateUtils.common2Baidu(new LatLng(33.144513, 103.910688));
|
|
|
- mUASLocation.setGpsLatitude(address.getLatitude());
|
|
|
- mUASLocation.setGpsLongitude(address.getLongitude());
|
|
|
- mUASLocation.setLatitude(latlng.latitude);
|
|
|
- mUASLocation.setLongitude(latlng.longitude);
|
|
|
- Log.i(TAG,"国外定位:"+mUASLocation.toString());
|
|
|
- if (mOnLocationListener != null) {
|
|
|
- mOnLocationListener.onReceiveLocation(mUASLocation);
|
|
|
- }
|
|
|
- release();
|
|
|
}
|
|
|
|
|
|
private void clearLocation() {
|
|
|
@@ -116,20 +152,46 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
|
|
|
@Override
|
|
|
public void release() {
|
|
|
- mLocationManager.removeUpdates(locationListener);
|
|
|
+ if (mLocationManager != null) {
|
|
|
+ mLocationManager.removeUpdates(locationListener);
|
|
|
+ }
|
|
|
+ if (mLocationClient != null && mLocationClient.isStarted()) {
|
|
|
+ mLocationClient.stop();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public void requestLocation(OnLocationListener mOnLocationListener) {
|
|
|
- if (mLocationManager == null || ActivityCompat.checkSelfPermission(ct, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(ct, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ public void requestLocation(final OnLocationListener mOnLocationListener) {
|
|
|
+ if (mLocationManager == null) {
|
|
|
return;
|
|
|
}
|
|
|
+ if (Looper.getMainLooper() == Looper.myLooper()) {
|
|
|
+ requestLocationUI(mOnLocationListener);
|
|
|
+ } else {
|
|
|
+ mMainHandler.post(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ requestLocationUI(mOnLocationListener);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mLocationClient != null) {
|
|
|
+ if (!mLocationClient.isStarted()) {
|
|
|
+ mLocationClient.start();
|
|
|
+ } else {
|
|
|
+ mLocationClient.requestLocation();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void requestLocationUI(OnLocationListener mOnLocationListener) {
|
|
|
+ if (ActivityCompat.checkSelfPermission(ct, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(ct, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
|
|
|
+ return;
|
|
|
this.mOnLocationListener = mOnLocationListener;
|
|
|
isUpdateLocation = false;
|
|
|
String provider = getWellProvider(mLocationManager);
|
|
|
- //获取最后一次定位
|
|
|
-// Location location = locationManager.getLastKnownLocation(provider);
|
|
|
//刷新定位
|
|
|
mLocationManager.requestLocationUpdates(provider, 10000, 0, locationListener);
|
|
|
}
|
|
|
@@ -145,6 +207,36 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private BDLocationListener mLocationListener = new BDLocationListener() {
|
|
|
+ @Override
|
|
|
+ public void onReceiveLocation(BDLocation location) {
|
|
|
+ try {
|
|
|
+ String CoorType = location.getCoorType();
|
|
|
+ Log.i(TAG, "CoorType=" + CoorType);
|
|
|
+ String name = location.getLocationDescribe();
|
|
|
+ String address = location.getAddrStr();
|
|
|
+ double latitude = location.getLatitude();
|
|
|
+ double longitude = location.getLongitude();
|
|
|
+ if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(address) && latitude * longitude != 0) {
|
|
|
+ setLocation(CoorType, location);
|
|
|
+ } else if (latitude * longitude != 0) {
|
|
|
+ Geocoder gc = new Geocoder(ct, Locale.getDefault());
|
|
|
+ List<Address> locationList = null;
|
|
|
+ try {
|
|
|
+ locationList = gc.getFromLocation(latitude, longitude, 1000);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (locationList != null && locationList.size() > 0) {
|
|
|
+ isUpdateLocation = true;
|
|
|
+ setLocation(UASLocation.TYPE_BAIDU, CoorType, locationList.get(0));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ clearLocation();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
private LocationListener locationListener = new LocationListener() {
|
|
|
//当位置改变的时候调用
|
|
|
@Override
|
|
|
@@ -165,7 +257,7 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
if (locationList != null && locationList.size() > 0) {
|
|
|
isUpdateLocation = true;
|
|
|
Address address = locationList.get(0);//得到Address实例
|
|
|
- setLocation(address);
|
|
|
+ setLocation(UASLocation.TYPE_NATIVE, "gps", address);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -201,6 +293,73 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
};
|
|
|
|
|
|
|
|
|
+ private void setLocation(String type, BDLocation location) throws Exception {
|
|
|
+ if (location.getLocType() == BDLocation.TypeGpsLocation// GPS定位结果
|
|
|
+ || location.getLocType() == BDLocation.TypeNetWorkLocation//网络定位
|
|
|
+ || location.getLocType() == BDLocation.TypeOffLineLocation//离线定位(未验证离线定位的有效性)
|
|
|
+ ) {
|
|
|
+ //定位成功
|
|
|
+ mLocationClient.stop();
|
|
|
+ if (mUASLocation == null) {
|
|
|
+ mUASLocation = new UASLocation(UASLocation.TYPE_BAIDU);
|
|
|
+ } else {
|
|
|
+ clearLocation();
|
|
|
+ }
|
|
|
+ mUASLocation.setLocationOk(true);
|
|
|
+ changeCoord(type, new LatLng(location.getLatitude(), location.getLongitude()));
|
|
|
+ mUASLocation.setProvince(location.getProvince());
|
|
|
+ mUASLocation.setCityName(location.getCity());
|
|
|
+ mUASLocation.setAddress(location.getAddrStr());
|
|
|
+ mUASLocation.setName(location.getLocationDescribe());
|
|
|
+ isUpdateLocation = true;
|
|
|
+ } else {
|
|
|
+ if (mUASLocation == null) {
|
|
|
+ mUASLocation = new UASLocation(UASLocation.TYPE_BAIDU);
|
|
|
+ } else {
|
|
|
+ clearLocation();
|
|
|
+ }
|
|
|
+ //统一为定位失败
|
|
|
+ mUASLocation.setLocationOk(false);
|
|
|
+ String errorMessage = null;
|
|
|
+ if (location.getLocType() == BDLocation.TypeServerError) {
|
|
|
+ //服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因
|
|
|
+ errorMessage = "服务端网络定位失败";
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
|
|
|
+ //网络不同导致定位失败,请检查网络是否通畅
|
|
|
+ errorMessage = "网络不同导致定位失败,请检查网络是否通畅";
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
|
|
|
+ //无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机
|
|
|
+ errorMessage = "无法获取有效定位依据导致定位失败";
|
|
|
+ } else {
|
|
|
+ errorMessage = "未知错误";
|
|
|
+ }
|
|
|
+ mUASLocation.setRemarks(errorMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void changeCoord(String type, LatLng mLatLng) {
|
|
|
+ if (mLatLng == null) return;
|
|
|
+ LatLng latlng = null;
|
|
|
+ if (type != null && !"BD09LL".equals(type.toUpperCase())) {
|
|
|
+ if (type.toUpperCase().equals("GCJ02")) {
|
|
|
+ latlng = CoordinateUtils.common2Baidu(mLatLng);
|
|
|
+ } else {
|
|
|
+ latlng = CoordinateUtils.gps2Baidu(mLatLng);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ mUASLocation.setGpsLatitude(mLatLng.latitude);
|
|
|
+ mUASLocation.setGpsLongitude(mLatLng.longitude);
|
|
|
+ if (latlng == null) {
|
|
|
+ mUASLocation.setLatitude(mLatLng.latitude);
|
|
|
+ mUASLocation.setLongitude(mLatLng.longitude);
|
|
|
+ } else {
|
|
|
+ mUASLocation.setLatitude(latlng.latitude);
|
|
|
+ mUASLocation.setLongitude(latlng.longitude);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private HttpClient getHttpClient() {
|
|
|
if (mHttpClient == null) {
|
|
|
mHttpClient = new HttpClient.Builder("https://maps.googleapis.com/maps/api/place/").isDebug(false)
|
|
|
@@ -254,28 +413,6 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
}
|
|
|
|
|
|
|
|
|
-// /**
|
|
|
-// * 由于google收费,必须限制每天使用量
|
|
|
-// * 流程:
|
|
|
-// * 1.第一次进入直接进行搜索
|
|
|
-// */
|
|
|
-// private String lastKey;
|
|
|
-// private boolean isWhating, isResulting = false;
|
|
|
-// private Handler mWhatHandler = new Handler(Looper.getMainLooper());
|
|
|
-//
|
|
|
-// private void startWhat(final Context ct, final OnSearchLocationListener mOnSearchLocationListener) {
|
|
|
-// if (!isWhating) {
|
|
|
-// isWhating = true;
|
|
|
-// mWhatHandler.postAtTime(new Runnable() {
|
|
|
-// @Override
|
|
|
-// public void run() {
|
|
|
-// isWhating = false;
|
|
|
-// loadByInput(ct, null, lastKey, mOnSearchLocationListener);
|
|
|
-// }
|
|
|
-// }, 1000);
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
/**
|
|
|
* 通过输入进行搜索
|
|
|
*
|
|
|
@@ -287,17 +424,10 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
public void loadByInput(Context ct, String city, String keyWord, final OnSearchLocationListener mOnSearchLocationListener) {
|
|
|
if (ct == null || mOnSearchLocationListener == null)
|
|
|
return;
|
|
|
-// this.lastKey = keyWord;
|
|
|
-// if (isWhating || isResulting) {
|
|
|
-// startWhat(ct, mOnSearchLocationListener);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// isResulting = true;
|
|
|
HttpClient.Builder requestBuilder = new HttpClient.Builder()
|
|
|
.isDebug(true)
|
|
|
.url("textsearch/json")
|
|
|
.add("query", keyWord)
|
|
|
-// .add("region", city)//城市
|
|
|
.method(Method.GET)
|
|
|
.add("key", ct.getString(R.string.app_google_key));
|
|
|
HttpClient request = requestBuilder.build();
|
|
|
@@ -316,7 +446,6 @@ public class NativeLocationManager implements BaseLocationManager {
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
} finally {
|
|
|
-// isResulting = false;
|
|
|
}
|
|
|
}
|
|
|
|