高德

高德

  • 显示地图
  • 显示蓝标
  • 获得本地经纬度,加载图片到地图上
  • 导航

显示地图

public class MainActivity extends AppCompatActivity {

    MapView mapView;
    AMap aMap;
    MyLocationStyle myLocationStyle;


    String[] string=new String[]{
            "android.permission.INTERNET",
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.ACCESS_NETWORK_STATE",
            "android.permission.ACCESS_WIFI_STATE",
            "android.permission.READ_PHONE_STATE",
            "android.permission.ACCESS_COARSE_LOCATION",
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.CHANGE_WIFI_STATE",
            "android.permission.WAKE_LOCK",
            "android.permission.BLUETOOTH_ADMIN",
            "android.permission.BLUETOOTH",
            "android.permission.CALL_PHONE",
            "android.permission.CAMERA",
            "android.permission.READ_CONTACTS",
            "android.permission.WRITE_CONTACTS"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(string,110);
        }

        init();

        mapView.onCreate(savedInstanceState);
        if(aMap==null){
            aMap=mapView.getMap();
        }


    }

    private void init() {
        mapView=findViewById(R.id.mapview);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        mapView.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);
    }

  
}

显示蓝标

        //蓝点坐标
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        //aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示,非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。

获得本地经纬度,加载图片到地图上

 double latitude = aMap.getMyLocation().getLatitude();

        double longitude = aMap.getMyLocation().getLongitude();
        double altitude = aMap.getMyLocation().getAltitude();
        Log.i("yu", "latitude: "+latitude+"   longitude: "+longitude+"   altitude: "+altitude);

        //绘制点标记
        LatLng latLng = new LatLng(latitude,longitude);
        MarkerOptions options = new MarkerOptions()
                .position(latLng)
                .title("北京")
                .snippet("DefaultMarker")
                .draggable(true) //可拖动
                .setFlat(true) //marker平贴地图的效果
                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.a)));
        aMap.addMarker(options);

导航

public class MainActivity extends AppCompatActivity  {

    AMapNaviView mAMapNaviView;
    AMapNavi aMapNavi;

    EditText start_text,end_text;

    AMap aMap;

    List<NaviLatLng>  start=new ArrayList<>();
    List<NaviLatLng>  end=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAMapNaviView = findViewById(R.id.amap);
        start_text=findViewById(R.id.edit_start);
        end_text=findViewById(R.id.edit_end);

        aMap = mAMapNaviView.getMap();
        blue();
        mAMapNaviView.onCreate(savedInstanceState);

        aMapNavi=AMapNavi.getInstance(this);

        aMapNavi.setUseInnerVoice(true);
        aMapNavi.addAMapNaviListener(new AMapNaviListener() {
            @Override
            public void onInitNaviFailure() {

            }

            @Override
            public void onInitNaviSuccess() {



            }

            @Override
            public void onStartNavi(int i) {

            }

            @Override
            public void onTrafficStatusUpdate() {

            }

            @Override
            public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

            }

            @Override
            public void onGetNavigationText(int i, String s) {

            }

            @Override
            public void onGetNavigationText(String s) {

            }

            @Override
            public void onEndEmulatorNavi() {

            }

            @Override
            public void onArriveDestination() {

            }

            @Override
            public void onCalculateRouteFailure(int i) {

            }

            @Override
            public void onReCalculateRouteForYaw() {

            }

            @Override
            public void onReCalculateRouteForTrafficJam() {

            }

            @Override
            public void onArrivedWayPoint(int i) {

            }

            @Override
            public void onGpsOpenStatus(boolean b) {

            }

            @Override
            public void onNaviInfoUpdate(NaviInfo naviInfo) {

            }

            @Override
            public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {

            }

            @Override
            public void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {

            }

            @Override
            public void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {

            }

            @Override
            public void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {

            }

            @Override
            public void showCross(AMapNaviCross aMapNaviCross) {

            }

            @Override
            public void hideCross() {

            }

            @Override
            public void showModeCross(AMapModelCross aMapModelCross) {

            }

            @Override
            public void hideModeCross() {

            }

            @Override
            public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

            }

            @Override
            public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {

            }

            @Override
            public void hideLaneInfo() {

            }

            @Override
            public void onCalculateRouteSuccess(int[] ints) {
                aMapNavi.startNavi(NaviType.GPS);

            }

            @Override
            public void notifyParallelRoad(int i) {

            }

            @Override
            public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

            }

            @Override
            public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

            }

            @Override
            public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {

            }

            @Override
            public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

            }

            @Override
            public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

            }

            @Override
            public void onPlayRing(int i) {

            }

            @Override
            public void onCalculateRouteSuccess(AMapCalcRouteResult aMapCalcRouteResult) {

            }

            @Override
            public void onCalculateRouteFailure(AMapCalcRouteResult aMapCalcRouteResult) {

            }

            @Override
            public void onNaviRouteNotify(AMapNaviRouteNotifyData aMapNaviRouteNotifyData) {

            }
        });



    }

    private void blue() {
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.interval(2000);
        aMap.setMyLocationStyle(myLocationStyle);
        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        aMap.setMyLocationEnabled(true);


    }

    @Override
    protected void onResume() {
        super.onResume();
        mAMapNaviView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAMapNaviView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAMapNaviView.onDestroy();
    }

    public void click(View view) {


        String start_1 = start_text.getText().toString();
        String end_1 = end_text.getText().toString();

        if (TextUtils.isEmpty(start_1)&&TextUtils.isEmpty(end_1)) {
            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
        }else{
            if (TextUtils.isEmpty(start_1)) {
                Log.i("yu", "自己的位置纬度: ");

                double latitude = mAMapNaviView.getMap().getMyLocation().getLatitude();
                double longitude = mAMapNaviView.getMap().getMyLocation().getLongitude();

                Log.i("yu", "自己的位置纬度: "+latitude);
                Log.i("yu", "自己的位置经度: "+longitude);
                start.add(new NaviLatLng(latitude,longitude));
                poi(null,end_1);

            }else{
                poi(start_1,end_1);


            }

        }

    }
    public void poi(final String start_1, String end_1){


        PoiSearch.Query query = new PoiSearch.Query(start_1, "", "");
        query.setPageSize(1);

        PoiSearch poiSearch = new PoiSearch(this, query);
        poiSearch.searchPOIAsyn();

        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                if (poiResult!=null && i==1000) {
                    ArrayList<PoiItem> pois = poiResult.getPois();
                    for (PoiItem poiItem : pois) {
                        String title = poiItem.getTitle();
                        LatLonPoint latLonPoint = poiItem.getLatLonPoint();
                        Log.i("yu", "start纬度: "+latLonPoint.getLatitude());
                        Log.i("yu", "start经度: "+latLonPoint.getLongitude());
                        Log.i("yu", "start title: "+title);
                        start.clear();
                        start.add(new NaviLatLng(latLonPoint.getLatitude(),latLonPoint.getLongitude()));


                    }
                }
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });
        PoiSearch.Query query1 = new PoiSearch.Query(end_1, "", "");
        query.setPageSize(1);

        PoiSearch poiSearch1 = new PoiSearch(this, query1);
        poiSearch1.searchPOIAsyn();
        poiSearch1.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                if (poiResult!=null && i==1000) {
                    ArrayList<PoiItem> pois = poiResult.getPois();
                    for (PoiItem poiItem : pois) {
                        String title = poiItem.getTitle();
                        LatLonPoint latLonPoint = poiItem.getLatLonPoint();
                        Log.i("yu", "end纬度: "+latLonPoint.getLatitude());
                        Log.i("yu", "end经度: "+latLonPoint.getLongitude());
                        Log.i("yu", "end title: "+title);

                        end.clear();
                        end.add(new NaviLatLng(latLonPoint.getLatitude(),latLonPoint.getLongitude()));

                        int strategy=0;
                        try {
                            strategy = aMapNavi.strategyConvert(true, false, false, false, false);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        aMapNavi.calculateDriveRoute(start, end, null, strategy);

                    }
                }

            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });



    }
}

你可能感兴趣的:(高德)