高德导航

导入依赖
//定位功能
implementation ‘com.amap.api:location:latest.integration’
//搜索功能
implementation ‘com.amap.api:search:latest.integration’
implementation ‘com.amap.api:navi-3dmap:latest.integration’

清单文件权限

 
 
 
 
 



    

package com.example.gaodemap;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapCalcRouteResult;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviRouteNotifyData;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.autonavi.tbt.TrafficFacilityInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements AMapNaviListener {

private static final String TAG = "MainActivity";
private EditText mStart;
private EditText mEnd;
private Button mNavi;
private AMapNaviView mAMapNaviView;
private AMapNavi aMapNavi;
private ArrayList sList=new ArrayList<>();
NaviLatLng start=new NaviLatLng();
private ArrayList eList=new ArrayList<>();
NaviLatLng end=new NaviLatLng();
private PoiSearch.Query query;
boolean isint;
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;


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


    mStart = findViewById(R.id.start);
    mEnd = findViewById(R.id.end);
    mNavi = findViewById(R.id.navi);
    mAMapNaviView = findViewById(R.id.aMapNaviView);

    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},100);
    }
    mAMapNaviView.onCreate(savedInstanceState);
    initAmap();

}



private void initAmap() {
    aMapNavi = AMapNavi.getInstance(getApplicationContext());
    aMapNavi.addAMapNaviListener(this);
    aMapNavi.setUseInnerVoice(true);
}

//声明定位回调监听器
public AMapLocationListener mLocationListener = new AMapLocationListener() {
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //定位成功回调信息,设置相关消息
                aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
                double latitude = aMapLocation.getLatitude();//获取纬度
                double longitude = aMapLocation.getLongitude();//获取经度
                aMapLocation.getAccuracy();//获取精度信息
                Log.i(TAG, "onLocationChanged: 纬度:"+latitude+",经度:"+longitude+",位置:"+aMapLocation.getAddress());
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(aMapLocation.getTime());
                df.format(date);//定位时间
            } else {
                //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                Log.e("AmapError","location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }
    }
};
@Override
public void onInitNaviFailure() {
    Toast.makeText(this, "初始化失败", Toast.LENGTH_SHORT).show();
}

// TODO:初始化成功
@Override
public void onInitNaviSuccess() {

    isint=true;
    Toast.makeText(this, "初始化成功", Toast.LENGTH_SHORT).show();
}

@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) {

}

public void click(View view) {

        search();

}

private void search() {
        if(isint){
            String start = mStart.getText().toString().trim();
            String end = mEnd.getText().toString().trim();
            if(TextUtils.isEmpty(start)||TextUtils.isEmpty(end)){
                Toast.makeText(this, "不能尾空", Toast.LENGTH_SHORT).show();
            }else{
                //计算经纬度
                poisearch(start,0);
                poisearch(end,1);


            }
        }else{
            Toast.makeText(this, "初始化失败", Toast.LENGTH_SHORT).show();
        }
}

private void poisearch(String keyword,final int type) {
    query = new PoiSearch.Query(keyword, "", "北京市");
    //keyWord表示搜索字符串,
    //第二个参数表示POI搜索类型,二者选填其一,选用POI搜索类型时建议填写类型代码,码表可以参考下方(而非文字)
    //cityCode表示POI搜索区域,可以是城市编码也可以是城市名称,也可以传空字符串,空字符串代表全国在全国范围内进行搜索
    query.setPageSize(1);// 设置每页最多返回多少条poiitem
    query.setPageNum(1);//设置查询页码

    PoiSearch poiSearch = new PoiSearch(this, query);
    poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
        @Override
        public void onPoiSearched(PoiResult poiResult, int i) {
            if (poiResult != null && i == 1000) {
                ArrayList pois = poiResult.getPois();
                PoiItem poiItem = pois.get(0);
                Log.d("ytx", "纬度: " + poiItem.getLatLonPoint().getLatitude());
                Log.d("ytx", "经度: " + poiItem.getLatLonPoint().getLongitude());
                Log.d("ytx", "title: " + poiItem.getTitle());
                Log.d("ytx", "电话: " + poiItem.getTel());
                if (type == 0) {
                    start.setLatitude(poiItem.getLatLonPoint().getLatitude());
                    start.setLongitude(poiItem.getLatLonPoint().getLongitude());
                    sList.clear();
                    sList.add(start);
                } else {
                    end.setLatitude(poiItem.getLatLonPoint().getLatitude());
                    end.setLongitude(poiItem.getLatLonPoint().getLongitude());
                    eList.clear();
                    eList.add(end);
                    int strategy = aMapNavi.strategyConvert(true, false, false, false, false);
                    sList.add(start);
                    eList.add(end);
                    aMapNavi.calculateDriveRoute(sList, eList, null, strategy);
                }
            }
        }
        @Override
        public void onPoiItemSearched(PoiItem poiItem, int i) {

        }
    });
    poiSearch.searchPOIAsyn();
}

}

布局


    
        
        
    

    

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