本文共 3323 字,大约阅读时间需要 11 分钟。
和其他地图一样,都要先去官网注册成为开发者,然后获取Key。下面直接上代码。
效果图:
package com.example.gaodemap;import com.amap.api.maps.AMap;import com.amap.api.maps.CameraUpdate;import com.amap.api.maps.CameraUpdateFactory;import com.amap.api.maps.MapView;import com.amap.api.maps.model.LatLng;import com.amap.api.maps.model.Marker;import com.amap.api.maps.model.MarkerOptions;import android.app.Activity;import android.content.Context;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.ToggleButton;public class MainActivity extends Activity { private MapView mMapView; private AMap aMap; private MapView mapView; private LocationManager locationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); init(); //GPRS提供的定位信息改变 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 8, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // 使用GPRS提供的定位信息来更新位置 updatePosition(locationManager.getLastKnownLocation(provider)); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub updatePosition(location); } }); ToggleButton tb = (ToggleButton) findViewById(R.id.tb); tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ aMap.setMapType(AMap.MAP_TYPE_SATELLITE); }else{ aMap.setMapType(AMap.MAP_TYPE_NORMAL); } } }); } //初始化AMap对象 private void init(){ if(aMap == null){ aMap = mMapView.getMap(); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mMapView.onDestroy(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); mMapView.onPause(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); mMapView.onResume(); } @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); } private void updatePosition(Location location){ LatLng pos = new LatLng(location.getLatitude(), location.getLongitude()); //创建一个设置经纬度的CameraUpdate CameraUpdate cu = CameraUpdateFactory.changeLatLng(pos); //更新地图的显示区域 aMap.moveCamera(cu); //清除所有的Marker等覆盖物 aMap.clear(); //创建一个MarkerOptions对象 MarkerOptions markOptions = new MarkerOptions(); markOptions.position(pos); //添加MarkerOptions(实际上是添加Marker) Marker marker = aMap.addMarker(markOptions); } }