高德地图 坐标详解(常用的几种在线地图)

一、坐标系介绍

高德地图使用的是WGS84坐标系和GCJ-02坐标系。

其中,WGS84坐标系是全球通用的地球坐标系,由于计算简单,精度高,因此广泛使用。

而GCJ-02坐标系则是为了解决中国特有的需求而提出的坐标系,它是在WGS84坐标系基础上将坐标信息进行了加密处理,提高了数据的安全性。

二、坐标转换

由于不同的应用场景需要使用不同的坐标系,因此在实际开发过程中,我们需要进行坐标系的转换。

下面是一个将WGS84坐标系转换成GCJ-02坐标系的示例代码:

public static double[] wgs84ToGcj02(double lng, double lat) {
    double[] result = new double[2];
    double pi = 3.1415926535897932384626;
    double a = 6378245.0;
    double ee = 0.00669342162296594323;
    double dLng = transformLng(lng - 105.0, lat - 35.0);
    double dLat = transformLat(lng - 105.0, lat - 35.0);
    double radLat = lat / 180.0 * pi;
    double magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = Math.sqrt(magic);
    dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    result[0] = lng + dLng;
    result[1] = lat + dLat;
    return result;
}

在进行坐标系转换时,为了避免数据遗失,要保证转换前后的坐标系保持一致。

三、地图显示

高德地图坐标可以直接用于地图显示。在进行地图显示前,需要调用API获取地图数据和实例化地图。

下面是一个获取地图数据和实例化地图的示例代码:

// 获取地图数据
Map params = new HashMap();
params.put("key", "your_key");
params.put("location", "116.397428,39.90923");
params.put("radius", "1000");
params.put("extensions", "all");
String result = HttpUtils.sendGet("https://restapi.amap.com/v3/place/around", params);

// 实例化地图
MapView mapView = (MapView) findViewById(R.id.map_view);
AMap aMap = mapView.getMap();
aMap.setMapTextZIndex(2);
aMap.showIndoorMap(true);
aMap.getUiSettings().setZoomControlsEnabled(false);
aMap.setMapType(AMap.MAP_TYPE_NORMAL);

四、地理编码和逆地理编码

高德地图提供了地理编码和逆地理编码功能。其中,地理编码将地址信息转换成地理坐标,逆地理编码将地理坐标转换成地址信息。

下面是一个进行地理编码的示例代码:

GeocodeSearch geocoderSearch = new GeocodeSearch(context);
geocoderSearch.setOnGeocodeSearchListener(new OnGeocodeSearchListener() {
    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        // 逆地理编码结果回调
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
        // 地理编码结果回调
        if (i == AMapException.CODE_AMAP_SUCCESS) {
            if (geocodeResult != null && geocodeResult.getGeocodeAddressList() != null
                    && geocodeResult.getGeocodeAddressList().size() > 0) {
                GeocodeAddress address = geocodeResult.getGeocodeAddressList().get(0);
                String formatAddress = address.getFormatAddress();
                String province = address.getProvince();
                String city = address.getCity();
                String district = address.getDistrict();
                String township = address.getTownship();
                String adCode = address.getAdCode();
            }
        }
    }
});

GeocodeQuery query = new GeocodeQuery("北京市海淀区上地十街10号", "北京市");
geocoderSearch.getFromLocationNameAsyn(query);

上述代码将地址信息“北京市海淀区上地十街10号”转换成了地理坐标,并获取了详细的地址信息。

五、路径规划

高德地图提供了路径规划功能,包括驾车路径规划、公交路径规划、步行路径规划和骑行路径规划。

下面是一个进行驾车路径规划的示例代码:

RouteSearch routeSearch = new RouteSearch(context);
routeSearch.setRouteSearchListener(new OnRouteSearchListener() {
    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
        // 驾车路径规划结果回调
        if (i == AMapException.CODE_AMAP_SUCCESS) {
            if (driveRouteResult != null && driveRouteResult.getPaths() != null
                    && driveRouteResult.getPaths().size() > 0) {
                DrivePath drivePath = driveRouteResult.getPaths().get(0);
                int duration = drivePath.getDuration();
                float distance = drivePath.getDistance();
                List steps = drivePath.getSteps();
            }
        }
    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
        // 公交路径规划结果回调
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
        // 步行路径规划结果回调
    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {
        // 骑行路径规划结果回调
    }
});

DriveRouteQuery query = new DriveRouteQuery(from, to, null, null, null);
routeSearch.calculateDriveRouteAsyn(query);

上述代码将起点和终点传入路径规划函数中,进行驾车路径规划,并获取了详细的路径信息。

六、地图搜索

高德地图提供了地图搜索功能,包括周边搜索、关键字搜索、ID搜索和公交站点搜索。

下面是一个进行周边搜索的示例代码:

PoiSearch.Query query = new PoiSearch.Query("美食", "", city);
query.setPageSize(10);
query.setPageNum(0);
PoiSearch poiSearch = new PoiSearch(context, query);
poiSearch.setOnPoiSearchListener(new OnPoiSearchListener() {
    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        // 地图搜索结果回调
        if (i == AMapException.CODE_AMAP_SUCCESS) {
            if (poiResult != null && poiResult.getPois() != null
                    && poiResult.getPois().size() > 0) {
                List items = poiResult.getPois();
                for (PoiItem item : items) {
                    String name = item.getTitle();
                    String address = item.getSnippet();
                    String type = item.getTypeDes();
                    LatLonPoint point = item.getLatLonPoint();
                    double lat = point.getLatitude();
                    double lng = point.getLongitude();
                }
            }
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {
        // 单个地点搜索结果回调
    }
});

LatLonPoint centerPoint = new LatLonPoint(lat, lng);
poiSearch.setBound(new PoiSearch.SearchBound(centerPoint, 1000));
poiSearch.searchPOIAsyn();

上述代码将搜索关键字“美食”,搜索范围为当前位置半径1km以内的地点,并获取了详细的地点信息。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平