Android从0到完整项目(7)地图与导航

百度地图或者高德地图 实现 与地图相关的功能

以高德地图为例,百度地图API 基本类似,看后台要求,经纬度要一致,消除转经纬度的问题。。

效果图

定位

Android从0到完整项目(7)地图与导航_第1张图片
定位

地图移动

Android从0到完整项目(7)地图与导航_第2张图片
转换地图中心点

对话框

Android从0到完整项目(7)地图与导航_第3张图片
对话框

导航

Android从0到完整项目(7)地图与导航_第4张图片
导航

高德地图 api: http://lbs.amap.com
百度地图API:http://lbsyun.baidu.com/index.php?title=androidsdk

定位辅助类

public class LocationProvider {
    public AMapLocationClient getLocationClient() {
        return locationClient;
    }

    public void setLocationClient(AMapLocationClient locationClient) {
        this.locationClient = locationClient;
    }

    private AMapLocationClient locationClient = null;
    //    private LocationClientOption mOption,DIYoption;
    private Object objLock = new Object();
    private AMapLocationClientOption DIYoption;

    /***
     * @param locationContext
     */
    public LocationProvider(Context locationContext) {
        synchronized (objLock) {
            if (locationClient == null) {
                locationClient = new AMapLocationClient(locationContext);
                locationClient.setLocationOption(getDefaultOption());
            }
        }
    }

    /**
     * 默认的定位参数
     *
     * @author hongming.wangLocationProvider
     * @since 2.8.0
     */
    public AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(true);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(20000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(2000);//可选,设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
        return mOption;
    }


    /**
     * 销毁定位
     *
     * @author hongming.wang
     * @since 2.8.0
     */
    public void destroyLocation() {
        if (null != locationClient) {
            /**
             * 如果AMapLocationClient是在当前Activity实例化的,
             * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            locationClient.onDestroy();
            locationClient = null;


        }
    }


    public boolean registerListener(AMapLocationListener locationListener) {
        boolean isSuccess = false;
        if (locationListener != null) {
            locationClient.setLocationListener(locationListener);
            isSuccess = true;
        }
        return isSuccess;
    }

    public void unregisterListener(AMapLocationListener locationListener) {
        if (locationListener != null) {
            locationClient.unRegisterLocationListener(locationListener);
        }
    }


    /***
     * @param option
     * @return isSuccessSetOption
     */
    public boolean setLocationOption(AMapLocationClientOption option) {
        boolean isSuccess = false;
        if (option != null) {
            if (locationClient.isStarted())
                locationClient.stopLocation();
            DIYoption = option;
            locationClient.setLocationOption(option);
            isSuccess = true;
        }
        return isSuccess;
    }


    /**
     * 开始定位
     *
     * @author hongming.wang
     * @since 2.8.0
     */
    public void start() {
        synchronized (objLock) {
            if (locationClient != null && !locationClient.isStarted()) {
                locationClient.startLocation();
            }
        }
    }


    /**
     * 停止定位
     *
     * @author hongming.wang
     * @since 2.8.0
     */
    public void stop() {
        synchronized (objLock) {
            if (locationClient != null && locationClient.isStarted()) {
                locationClient.stopLocation();
            }
        }
    }


}

地图移动 改变中心点


aMap.animateCamera(CameraUpdateFactory.newLatLngZoom((LatLng) hotAreaList.get(position), 14));
                addMarkersToMap((LatLng) hotAreaList.get(position), position + 1);

显示自定义对话框

在界面中做好自己的View 然后显示隐藏就可以了。

  /**
     * 根据info为布局上的控件设置信息
     *
     * @param
     * @param
     */
    protected void popupInfo(ViewGroup mMarkerLy, int
            index) {

        mMarkerLy.setVisibility(View.VISIBLE);

        TextView parkAddressText = (TextView) mMarkerLy
                .findViewById(R.id.parkAddressText);
        TextView canUseParkNumberText = (TextView) mMarkerLy
                .findViewById(R.id.canUseParkNumberText);
        TextView distanceText = (TextView) mMarkerLy
                .findViewById(R.id.distanceText);

        TextView supportOnlineText = (TextView) mMarkerLy
                .findViewById(R.id.supportOnlineText);


        TextView perForTimeText = (TextView) mMarkerLy
                .findViewById(R.id.perForTimeText);

        Button goHereButton = (Button) mMarkerLy
                .findViewById(R.id.goHereButton);
        LinearLayout infoLayout = (LinearLayout) mMarkerLy.findViewById(R.id.parkDetialLineraLayout);


//        final int finalIndex = index;
//        final ParkInfo info = nearList.get(finalIndex);

        parkAddressText.setText("停车场地址");
        perForTimeText.setText("停车场距离");
//
//        DecimalFormat decimalFormat = new DecimalFormat("0.0");//构造方法的字符格式这里如果小数不足2位,会以0补足.
//        String p = decimalFormat.format(distance / 1000);//format 返回的是字符串
        distanceText.setText("3KM");
        canUseParkNumberText.setText("空车位数量");


        supportOnlineText.setText("支持线上支付");
        supportOnlineText.setVisibility(View.VISIBLE);


        goHereButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MapActivity.this, AMapNavBaseActivity.class);
                NaviLatLng endlatlng = new NaviLatLng(32.0414637353, 118.7851165312);
                NaviLatLng beginLatlng = new NaviLatLng(loctionLatLng.latitude,
                        loctionLatLng.longitude);
                Bundle bundle = new Bundle();
                bundle.putParcelable("beginLatlng", beginLatlng);
                bundle.putParcelable("endlatlng", endlatlng);
                intent.putExtras(bundle);
                startActivity(intent);
//                AbToastUtil.showToast(MapActivity.this, "导航");
            }
        });

        infoLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

//


            }
        });


    }

导航 根据官方Demo 编写调试

高德地图 需要自己申请科大讯飞 Key 做替换
高德地图 语音提示是 使用的第三方的语音库,在搭环境的时候要注意。

代码不定时更新 https://github.com/chinaltz/JustAndroid

你可能感兴趣的:(Android从0到完整项目(7)地图与导航)