欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Java CoordinateConverter类使用实例

最编程 2024-02-10 18:18:04
...

Java CoordinateConverter类使用实例

时间:2019-10-19
本文章向大家介绍Java CoordinateConverter类代码示例,你可以查看下面代码实例来了解Java CoordinateConverter类的使用方法及注意事项。文章结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

实例1: convert

import com.amap.api.location.CoordinateConverter; //导入依赖的package包/类
private void convert() {
	try{
		//初始化坐标转换类
		CoordinateConverter converter = new CoordinateConverter(
				getApplicationContext());
		/**
		 * 设置坐标来源,这里使用百度坐标作为示例
		 * 可选的来源包括:
		 * <li>CoordType.BAIDU : 百度坐标
		 * <li>CoordType.MAPBAR : 图吧坐标
		 * <li>CoordType.MAPABC : 图盟坐标
		 * <li>CoordType.SOSOMAP : 搜搜坐标
		 * <li>CoordType.ALIYUN : 阿里云坐标
		 * <li>CoordType.GOOGLE : 谷歌坐标
		 * <li>CoordType.GPS : GPS坐标
		 */
		converter.from(CoordType.BAIDU);
		//设置需要转换的坐标
		converter.coord(examplePoint);
		//转换成高德坐标
		DPoint destPoint = converter.convert();
		if(null != destPoint){
			tvConvertReult.setText("转换后坐标(经度、纬度):" + destPoint.getLongitude() + "," + destPoint.getLatitude());
		} else {
			Toast.makeText(getApplicationContext(), "坐标转换失败", Toast.LENGTH_SHORT).show();
		}
	}catch(Exception e){
		Toast.makeText(getApplicationContext(), "坐标转换失败", Toast.LENGTH_SHORT).show();
		e.printStackTrace();
	}
}
 

实例2: checkIsAvaliable

import com.amap.api.location.CoordinateConverter; //导入依赖的package包/类
private void checkIsAvaliable(){
	//初始化坐标工具类
	CoordinateConverter converter = new CoordinateConverter(
			getApplicationContext());
	//判断是否高德地图可用的坐标
	boolean result = converter.isAMapDataAvailable(examplePoint.getLatitude(), examplePoint.getLongitude());
	if(result){
		tvCheckReult.setText("该坐标是高德地图可用坐标");
	} else {
		tvCheckReult.setText("该坐标不能用于高德地图");
	}
}
 

实例3: amapLocationToObject

import com.amap.api.location.CoordinateConverter; //导入依赖的package包/类
private WritableMap amapLocationToObject(AMapLocation amapLocation) {
    WritableMap map = Arguments.createMap();
    Integer errorCode = amapLocation.getErrorCode();
    if (errorCode > 0) {
        map.putInt("errorCode", errorCode);
        map.putString("errorInfo", amapLocation.getErrorInfo());
    } else {
        Double latitude = amapLocation.getLatitude();
        Double longitude =  amapLocation.getLongitude();
        if (!needMars) {

            try {
                CoordinateConverter converter  = new CoordinateConverter(mReactContext);
                //返回true代表当前位置在大陆、港澳地区,反之不在。
                boolean isAMapDataAvailable = converter.isAMapDataAvailable(latitude,longitude);
                if (isAMapDataAvailable) {
                    // 在中国境内,火星了
                    double[] deltas = delta(latitude, longitude);
                    latitude = latitude - deltas[0];
                    longitude = longitude - deltas[1];
                }
            } catch (Exception ex) {
                return null;
            }
        }

        map.putInt("locationType", amapLocation.getLocationType());
        map.putDouble("latitude", latitude);
        map.putDouble("longitude", longitude);


        if (needDetail) {
            // GPS Only
            map.putDouble("accuracy", amapLocation.getAccuracy());
            map.putInt("satellites", amapLocation.getSatellites());
            map.putDouble("altitude", amapLocation.getAltitude());
            map.putDouble("speed", amapLocation.getSpeed());
            map.putDouble("bearing", amapLocation.getBearing());

            map.putString("address", amapLocation.getAddress());
            map.putString("adCode", amapLocation.getAdCode());
            map.putString("country", amapLocation.getCountry());
            map.putString("province", amapLocation.getProvince());
            map.putString("poiName", amapLocation.getPoiName());
            map.putString("provider", amapLocation.getProvider());
            map.putString("locationDetail", amapLocation.getLocationDetail());
            map.putString("street", amapLocation.getStreet());
            map.putString("streetNum", amapLocation.getStreetNum());
            map.putString("city", amapLocation.getCity());
            map.putString("cityCode", amapLocation.getCityCode());
            map.putString("country", amapLocation.getCountry());
            map.putString("district", amapLocation.getDistrict());
            // map.putString("aoiName", amapLocation.getAOIName());
        }

    }
   return map;
}
 

推荐阅读