实现地图上显示多mark点,自定义infoWindow,跳转高德地图和百度地图

这里写图片描述
实现功能如上图所示
1.需要绘制多个mark点,
2.可以点击生成一个自定义的infowindow
3.点击可以根据本机状况跳转高德地图或者百度地图


实现功能
一,绘制多个mark点
前提条件,我们继承好了高德地图和实现了地图绘制,实现了地图蓝点绘制

我们根据接口得到了网点的json数据
这里写图片描述
数据结构是这样的
于是我们解析下数据,

if(netWorkEntity.code.equals("2000")){
data = netWorkEntity.data;                           LoggerUtil.systemOut("AmapNetWorkEntity",netWorkEntity.toString());
if(data!=null){
    for(int i=0;i<data.size();i++){
        String position= data.get(i).position;
        String drivingSchoolName=data.get(i).latnames;
        String[] LatLng = positionToLatlng(position);
        Double Markerlongitude = Double.valueOf(LatLng[0].trim());
        Double Markerlatitude = Double.valueOf(LatLng[1].trim());
        aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
             .position(new LatLng(Markerlatitude, Markerlongitude))
             .title(drivingSchoolName)
             .draggable(true)                                            
             .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark3)));
                                }
                            }
                        }

在绘制mark点的时候就把,网点的name取出来,以后绘制infowindow的时候有用


二。给mark上绘制自定义的infowindow
在mark点上绘制点击事件

// 定义 Marker 点击事件监听
    AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
        // marker 对象被点击时回调的接口
        // 返回 true 则表示接口已响应事件,否则返回false
        @Override
        public boolean onMarkerClick(Marker marker) {
            marker.showInfoWindow(); // 显示点对应 的infowindow
            return false;

        }
    };
/**
     * 监听自定义infowindow窗口的infowindow事件回调
     */
    @Override
    public View getInfoWindow(Marker marker) {
        View    infoWindow=LayoutInflater.from(this).inflate(R.layout.map_bubble_back,null);//custom_info_window为自定义的layout文件
        TextView name=(TextView)infoWindow.findViewById(R.id.inforwindow_text);
        name.setText( marker.getTitle());
        return infoWindow;
    }

在重写父类的getInfoWindow方法中,我们用自定义的布局做背景,并且吧绘制mark点时候存入的网点名称取了出来

在infowindow的点击回调里边,我们判断当前时候安装了高德地图或者百度地图

  AMap.OnInfoWindowClickListener listener = new AMap.OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {
            if(isAvilible(MapActivity.this,"com.baidu.BaiduMap")&&isAvilible(MapActivity.this,"com.autonavi.minimap")){
                showPopupWindow(marker.getPosition().latitude,marker.getPosition().longitude);
            }else if(isAvilible(MapActivity.this,"com.baidu.BaiduMap")&&!isAvilible(MapActivity.this,"com.autonavi.minimap")){
                showPopupWindowBaiDu(marker.getPosition().latitude,marker.getPosition().longitude);
            }else if (!isAvilible(MapActivity.this,"com.baidu.BaiduMap")&&isAvilible(MapActivity.this,"com.autonavi.minimap")){
                showPopupWindowGaoDe(marker.getPosition().latitude,marker.getPosition().longitude);
            }else if(!isAvilible(MapActivity.this,"com.baidu.BaiduMap")&&!isAvilible(MapActivity.this,"com.autonavi.minimap")){
            }
        }
    };

跳转不同的popuwindow,
我们把mark点里边的经纬度取出来,赋值给popuwindow,

private void showPopupWindow(double latitude, double longitude) {
        LoggerUtil.i( "fenglatlon",latitude+"");
        LoggerUtil.i( "fenglatlon",longitude+"");
        markLongitude=longitude;
        markLatitude=latitude;
        View contentView= LayoutInflater.from(MapActivity.this).inflate(R.layout.popu_map,null);
        mPopWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        View map_gaode=(View)contentView.findViewById(R.id.map_gaode);
        View map_baidu=(View)contentView.findViewById(R.id.map_baidu);
        map_gaode.setOnClickListener(this);
        map_baidu.setOnClickListener(this);
        //显示popupwindow
        View rootview=LayoutInflater.from(MapActivity.this).inflate(R.layout.activity_map,null);
        mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        mPopWindow.showAtLocation(rootview, Gravity.CENTER,0,0);
    }

在popuwindow的创建之后,我们把这个mark点的经纬度取出来,掉时候传输给高德地图或者百度地图

下面是popuwindow两个item的点击事件,分别传递给了高德地图和百度地图

@Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.map_gaode:
                double a[]= GPSUtil.gcj02_To_Gps84(markLatitude,markLongitude);//lon 维度  lat 经度
                double lat=a[0];
                double lon=a[1];
                LoggerUtil.i("latlon",lat+"   "+lon);
                Uri mUri = Uri.parse("geo:"+lat+""+","+lon+""+"?q="+"鹏翔驾校");
                Intent mIntent = new Intent(Intent.ACTION_VIEW,mUri);
                startActivity(mIntent);
                break;
            case R.id.map_baidu:
                double b[]= GPSUtil.gcj02_To_Gps84(markLatitude,markLongitude);//lon 维度  lat 经度
                double lat2=b[0];
                double lon2=b[1];
                LoggerUtil.i("latlon",lat2+"   "+lon2);
                Uri mUri2 = Uri.parse("geo:"+lat2+""+","+lon2+""+"?q="+"鹏翔驾校");
                Intent mInten2t = new Intent(Intent.ACTION_VIEW,mUri2);
                startActivity(mInten2t);
                break;
            default:
                break;
        }
    }

这样子,我们这个绘制多个Mark点,并且自定义infowindow ,打开app实现导航的这个功能就算完成了,

下面给出整个类的代码,希望对大家有帮助,代码没有优化,望海涵

/**
 * 高德地图地图页面
 * @author fxr
 */
public class MapActivity extends AppCompatActivity implements LocationSource,AMapLocationListener,View.OnClickListener, AMap.InfoWindowAdapter {
    private String add="0.0,0.0";
    private List<NetWorkEntity.DataBean> data;
    private PopupWindow mPopWindow;
    //AMap是地图对象
    private AMap aMap;
    private MapView mapView;
    //声明AMapLocationClient类对象,定位发起端
    private AMapLocationClient mLocationClient = null;
    //声明mLocationOption对象,定位参数
    public AMapLocationClientOption mLocationOption = null;
    //声明mListener对象,定位监听器
    private OnLocationChangedListener mListener = null;
    //标识,用于判断是否只显示一次定位信息和用户重新定位
    private boolean isFirstLoc = true;

    private Double markLongitude;//mark点的经度
    private Double markLatitude;//mark点的纬度


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        //获取地图控件引用
        mapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
        mapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mapView.getMap();
            //设置显示定位按钮 并且可以点击
            UiSettings settings = aMap.getUiSettings();
            aMap.setLocationSource(this);//设置了定位的监听
            // 是否显示定位按钮
            settings.setMyLocationButtonEnabled(true);
            aMap.setMyLocationEnabled(true);//显示定位层并且可以触发定位,默认是flase
            MyLocationStyle myLocationStyle = new MyLocationStyle();
            myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色
            myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色  。
            aMap.setMyLocationStyle(myLocationStyle);
        }
        //开始定位
        location();
        // 绑定 Marker 被点击事件
        aMap.setOnMarkerClickListener(markerClickListener);
        aMap.setInfoWindowAdapter(this);
        aMap.setOnInfoWindowClickListener(listener);
    }

    private void location() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息(默认返回地址信息)
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI,默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false,不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        mapView.onDestroy();
        mLocationClient.stopLocation();//停止定位
        mLocationClient.onDestroy();//销毁定位客户端。
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
        mapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
    }

    @Override
    public void deactivate() {
        mListener = null;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                // 如果不设置标志位,此时再拖动地图时,它会不断将地图移动到当前的位置
                if (isFirstLoc) {
                    //设置缩放级别
                    aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()), 16, 0, 0)));
                    //将地图移动到定位点
                    aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
                    //点击定位按钮 能够将地图的中心移动到定位点
                    mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
                    isFirstLoc = false;
                    double latitude = aMapLocation.getLatitude();//获取纬度
                    double longitude =  aMapLocation.getLongitude();//获取经度
                    add=longitude+","+latitude;
                    getMoreNetDotFromNet();
                }
            } else {
                //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
                Toast.makeText(getApplicationContext(), "定位失败", Toast.LENGTH_LONG).show();
            }
        }
    }

    /**
     * 获得所有网点返回信息
     */
    private void getMoreNetDotFromNet() {
        Map<String, String> map = new HashMap<>();
        map.put("sign", ConfigConstants.SIGN);
        map.put("sposition",add);
        HttpManager.getInstence().getLearnCarService()
                .netWork(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NetWorkEntity>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(NetWorkEntity netWorkEntity) {
                        if(netWorkEntity.code.equals("2000")){
                            data = netWorkEntity.data;
                            LoggerUtil.systemOut("AmapNetWorkEntity",netWorkEntity.toString());
                            if(data!=null){
                                for(int i=0;i<data.size();i++){
                                    String position= data.get(i).position;
                                    String drivingSchoolName=data.get(i).latnames;
                                    String[] LatLng = positionToLatlng(position);
                                    Double Markerlongitude = Double.valueOf(LatLng[0].trim());
                                    Double Markerlatitude = Double.valueOf(LatLng[1].trim());
                                    aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                                            .position(new LatLng(Markerlatitude, Markerlongitude))
                                            .title(drivingSchoolName)
                                            .draggable(true)  //
                                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark3)));
                                }
                            }
                        }
                    }
                });
    }

    // 定义 Marker 点击事件监听
    AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
        // marker 对象被点击时回调的接口
        // 返回 true 则表示接口已响应事件,否则返回false
        @Override
        public boolean onMarkerClick(Marker marker) {
            marker.showInfoWindow(); // 显示点对应 的infowindow
            return false;

        }
    };


    //String 转数据
    private String[] positionToLatlng(String position) {
        String[] array = new String[5];
        array = position.split(",");
        return array;
    }
    private void showPopupWindow(double latitude, double longitude) {
        LoggerUtil.i( "fenglatlon",latitude+"");
        LoggerUtil.i( "fenglatlon",longitude+"");
        markLongitude=longitude;
        markLatitude=latitude;
        View contentView= LayoutInflater.from(MapActivity.this).inflate(R.layout.popu_map,null);
        mPopWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        View map_gaode=(View)contentView.findViewById(R.id.map_gaode);
        View map_baidu=(View)contentView.findViewById(R.id.map_baidu);
        map_gaode.setOnClickListener(this);
        map_baidu.setOnClickListener(this);
        //显示popupwindow
        View rootview=LayoutInflater.from(MapActivity.this).inflate(R.layout.activity_map,null);
        mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        mPopWindow.showAtLocation(rootview, Gravity.CENTER,0,0);
    }
    //只有百度地图
    private void showPopupWindowBaiDu(double latitude, double longitude) {
        LoggerUtil.i( "fenglatlon",latitude+"");
        LoggerUtil.i( "fenglatlon",longitude+"");
        markLongitude=longitude;
        markLatitude=latitude;
        View contentView= LayoutInflater.from(MapActivity.this).inflate(R.layout.popu_map,null);
        mPopWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        View map_gaode=(View)contentView.findViewById(R.id.map_gaode);
        map_gaode.setVisibility(View.GONE);
        View map_baidu=(View)contentView.findViewById(R.id.map_baidu);
//        map_gaode.setOnClickListener(this);
        map_baidu.setOnClickListener(this);
        //显示popupwindow
        View rootview=LayoutInflater.from(MapActivity.this).inflate(R.layout.activity_map,null);
        mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        mPopWindow.showAtLocation(rootview, Gravity.CENTER,0,0);
    }
    //只有高德地图
    private void showPopupWindowGaoDe(double latitude, double longitude) {
        LoggerUtil.i( "fenglatlon",latitude+"");
        LoggerUtil.i( "fenglatlon",longitude+"");
        markLongitude=longitude;
        markLatitude=latitude;
        View contentView= LayoutInflater.from(MapActivity.this).inflate(R.layout.popu_map,null);
        mPopWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        View map_gaode=(View)contentView.findViewById(R.id.map_gaode);
//        map_gaode.setVisibility(View.GONE);
        View map_baidu=(View)contentView.findViewById(R.id.map_baidu);
        map_baidu.setVisibility(View.GONE);
        map_gaode.setOnClickListener(this);
        map_baidu.setOnClickListener(this);
        //显示popupwindow
        View rootview=LayoutInflater.from(MapActivity.this).inflate(R.layout.activity_map,null);
        mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        mPopWindow.showAtLocation(rootview, Gravity.CENTER,0,0);
    }
    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     *          (34.347527, 108.945003)
     */
    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.map_gaode:
                double a[]= GPSUtil.gcj02_To_Gps84(markLatitude,markLongitude);//lon 维度  lat 经度
                double lat=a[0];
                double lon=a[1];
                LoggerUtil.i("latlon",lat+"   "+lon);
                Uri mUri = Uri.parse("geo:"+lat+""+","+lon+""+"?q="+"鹏翔驾校");
                Intent mIntent = new Intent(Intent.ACTION_VIEW,mUri);
                startActivity(mIntent);
                break;
            case R.id.map_baidu:
                double b[]= GPSUtil.gcj02_To_Gps84(markLatitude,markLongitude);//lon 维度  lat 经度
                double lat2=b[0];
                double lon2=b[1];
                LoggerUtil.i("latlon",lat2+"   "+lon2);
                Uri mUri2 = Uri.parse("geo:"+lat2+""+","+lon2+""+"?q="+"鹏翔驾校");
                Intent mInten2t = new Intent(Intent.ACTION_VIEW,mUri2);
                startActivity(mInten2t);
                break;
            default:
                break;
        }
    }
    /**
     * 检查手机上是否安装了指定的软件
     * @param context
     * @param packageName:应用包名
     * @return
     */
    public boolean isAvilible(Context context, String packageName){
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出,压入pName list中
        if(packageInfos != null){
            for(int i = 0; i < packageInfos.size(); i++){
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE
        return packageNames.contains(packageName);
    }
    /**
     * 监听自定义infowindow窗口的infocontents事件回调
     */
    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
    View infoWindow = null;
    /**
     * 监听自定义infowindow窗口的infowindow事件回调
     */
    @Override
    public View getInfoWindow(Marker marker) {
        View    infoWindow=LayoutInflater.from(this).inflate(R.layout.map_bubble_back,null);//custom_info_window为自定义的layout文件
        TextView name=(TextView)infoWindow.findViewById(R.id.inforwindow_text);
        name.setText( marker.getTitle());
        return infoWindow;
    }

    AMap.OnInfoWindowClickListener listener = new AMap.OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {
            if(isAvilible(MapActivity.this,"com.baidu.BaiduMap")&&isAvilible(MapActivity.this,"com.autonavi.minimap")){
                showPopupWindow(marker.getPosition().latitude,marker.getPosition().longitude);
            }else if(isAvilible(MapActivity.this,"com.baidu.BaiduMap")&&!isAvilible(MapActivity.this,"com.autonavi.minimap")){
                showPopupWindowBaiDu(marker.getPosition().latitude,marker.getPosition().longitude);
            }else if (!isAvilible(MapActivity.this,"com.baidu.BaiduMap")&&isAvilible(MapActivity.this,"com.autonavi.minimap")){
                showPopupWindowGaoDe(marker.getPosition().latitude,marker.getPosition().longitude);
            }else if(!isAvilible(MapActivity.this,"com.baidu.BaiduMap")&&!isAvilible(MapActivity.this,"com.autonavi.minimap")){
            }
        }
    };
}

对了,我们从内置高德地图中得到的mark点的经纬度,传递到高德地图app或者百度地图app 上的时候,需要吧经纬度进行编码转换
需要的操作是火星坐标系 (GCJ-02) to 84
参考资料

http://blog.csdn.net/a13570320979/article/details/51366355

给出工具类

**
 * Created by 冯昕睿 on 2017/11/3.
 * gps 编码格式转换类
 */

public class GPSUtil {
    public static double pi = 3.1415926535897932384626;
    public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    public static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    public static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return ret;
    }
    public static double[] transform(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new double[]{lat,lon};
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 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);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new double[]{mgLat,mgLon};
    }
    public static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }
    /**
     * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
     *
     * @param lat
     * @param lon
     * @return
     */
    public static double[] gps84_To_Gcj02(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new double[]{lat,lon};
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 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);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new double[]{mgLat, mgLon};
    }

    /**
     * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
     * */
    public static double[] gcj02_To_Gps84(double lat, double lon) {
        double[] gps = transform(lat, lon);
        double lontitude = lon * 2 - gps[1];
        double latitude = lat * 2 - gps[0];
        return new double[]{latitude, lontitude};
    }
    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
     *
     * @param lat
     * @param lon
     */
    public static double[] gcj02_To_Bd09(double lat, double lon) {
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta) + 0.0065;
        double tempLat = z * Math.sin(theta) + 0.006;
        double[] gps = {tempLat,tempLon};
        return gps;
    }

    /**
     * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
     * bd_lat * @param bd_lon * @return
     */
    public static double[] bd09_To_Gcj02(double lat, double lon) {
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta);
        double tempLat = z * Math.sin(theta);
        double[] gps = {tempLat,tempLon};
        return gps;
    }

    /**将gps84转为bd09
     * @param lat
     * @param lon
     * @return
     */
    public static double[] gps84_To_bd09(double lat,double lon){
        double[] gcj02 = gps84_To_Gcj02(lat,lon);
        double[] bd09 = gcj02_To_Bd09(gcj02[0],gcj02[1]);
        return bd09;
    }
    public static double[] bd09_To_gps84(double lat,double lon){
        double[] gcj02 = bd09_To_Gcj02(lat, lon);
        double[] gps84 = gcj02_To_Gps84(gcj02[0], gcj02[1]);
        //保留小数点后六位
        gps84[0] = retain6(gps84[0]);
        gps84[1] = retain6(gps84[1]);
        return gps84;
    }

    /**保留小数点后六位
     * @param num
     * @return
     */
    private static double retain6(double num){
        String result = String .format("%.6f", num);
        return Double.valueOf(result);
    }

}

好了高德地图的内容可能就到,这里了,因为,这个功能做出来了,还要去写其他模块233

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33413264/article/details/78469911
个人分类: android 高德地图
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭