高德
- 显示地图
- 显示蓝标
- 获得本地经纬度,加载图片到地图上
- 导航
显示地图
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();
mapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
显示蓝标
myLocationStyle = new MyLocationStyle();
myLocationStyle.interval(2000);
aMap.setMyLocationStyle(myLocationStyle);
aMap.setMyLocationEnabled(true);
获得本地经纬度,加载图片到地图上
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)
.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) {
}
});
}
}