不接入第三方sdk来实现手机坐标逆编码地理

一:android坐标常识

  • 世界坐标体系:WGS-84
    • 通过手机等gps设备获取到的坐标都是世界坐标体系坐标,android中通过手机原始api获取到的坐标就是WGS-84。但是通过手机地图等第三方sdk获取到的坐标则不是。
  • 中国火星坐标体系:GCJ-02
    • 高德地图/搜狗地图等都使用的是经过国家规定的一套加密算法后,对WGS-84原始坐标加密后生成的新的火星坐标。
  • 百度地理坐标体系:BD-09
    • 百度地图则是在对WGS-84坐标进行第一次加密生成GCJ-02坐标之后,在火星坐标基础之上通过百度自己的加密算法,再生成一套新的坐标体系。

需求:根据用户所在位置显示用户附近的医院

二:转换WGS-84为GCJ-02

1:准备工具
  • 下面是一个开源的各种语言坐标转换的算法工具
    https://github.com/googollee/eviltransform
  • 高德地图地理/逆地理编码
2:通过手机获取坐标的provider

手机获取坐标有三种途径,按照精度的高低分别是gps/wifi/基站。但是在android.location包下的LocationManager.java中,我们可以查阅到系统为我们提供了四种provider来获取坐标。

(1):NEW_PROVIDER 基于cell tower and wifi。
(2):GPS_PROVIDER 基于gps定位,可能耗时较长。
(3):PASSIVE_PROVIDER 基于其他应用请求的定位结果来更新。
(4):FUSED_PROVIDER 混合提供最优解。

3:获取坐标

(1):获取LocationManager

locationManager= (LocationManager) getSystemService(Context.LOCATION_SERVICE);

(2):获得坐标提供者
这里需要创建一个标准Criteria,locationManager将根据这个标准配置来返回最佳的坐标提供者

Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(true);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
bestProvider = locationManager.getBestProvider(criteria, true);

(3):注册坐标监听器,并在监听器的onLocationChanged(Location location)回调中保存最新的位置。

locationListener=new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        currentLocation=location;
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
};
locationManager.requestLocationUpdates(bestProvider,10,0,locationListener);

(4):获取最优的位置。考虑到用户关闭手机,或者尚未开通provider等情况,需要比较一个最优的位置。这里使用了一个isBetterLocation()方法。

Location gsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location netLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location bestLocation=gsLocation;
bestLocation = isBetterLocation(netLocation,bestLocation)?netLocation:bestLocation;
if(currentLocation!=null) {
    bestLocation = isBetterLocation(currentLocation, bestLocation) ? currentLocation : bestLocation;
}
double latitude = bestLocation.getLatitude();
double longitude = bestLocation.getLongitude();

(5):权限



(6):google提供的isBetterLocation()

private static final int TWO_MINUTES = 1000 * 60 * 2;

protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }
    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;
    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
        // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }
    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;
    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());
    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}


/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
        return provider2 == null;
    }
    return provider1.equals(provider2);
}

5:转换坐标

根据我们在上面列出的github开源项目,里面提供了一套java坐标转换算法。

WGSPointer wgsPointer=new WGSPointer(latitude,longitude);
GCJPointer gcjPointer = wgsPointer.toGCJPointer();
double latitude1 = gcjPointer.getLatitude();
double longitude1 = gcjPointer.getLongitude();

三:根据高德地图开放api实现坐标逆地理编码

http://restapi.amap.com/v3/geocode/regeo?key=您的key&location=116.508846,39.911447&poitype=商务写字楼&radius=1000&extensions=all&batch=false&roadlevel=0
不接入第三方sdk来实现手机坐标逆编码地理_第1张图片

你可能感兴趣的:(不接入第三方sdk来实现手机坐标逆编码地理)