BasicMapActivity
package com.example.gaodedemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.amap.api.maps.AMap; import com.amap.api.maps.MapView; import com.amap.api.maps.model.MyLocationStyle; public class BasicMapActivity extends AppCompatActivity implements View.OnClickListener { private MapView mMapView; private AMap aMap; private MyLocationStyle myLocationStyle; /** * 卫星图层 */ private Button mBtSatellite; /** * 夜间图层 */ private Button mBtNight; /** * 路况图层 */ private Button mBtTraffic; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_basic_map); initView(); //获取地图控件引用 mMapView = (MapView) findViewById(R.id.map); //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图 mMapView.onCreate(savedInstanceState); //初始化地图控制器对象 if (aMap == null) { aMap = mMapView.getMap(); } 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。 } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 mMapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 mMapView.onSaveInstanceState(outState); } private void initView() { mBtSatellite = (Button) findViewById(R.id.btSatellite); mBtSatellite.setOnClickListener(this); mBtNight = (Button) findViewById(R.id.btNight); mBtNight.setOnClickListener(this); mBtTraffic = (Button) findViewById(R.id.btTraffic); mBtTraffic.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.btSatellite: aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 设置卫星地图模式,aMap是地图控制器对象。 break; case R.id.btNight: aMap.setMapType(AMap.MAP_TYPE_NIGHT);// 设置夜间模式,aMap是地图控制器对象。 break; case R.id.btTraffic: aMap.setTrafficEnabled(true);//显示实时路况图层,aMap是地图控制器对象。 break; } } }
LocationActivity
package com.example.gaodedemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; 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.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import java.text.SimpleDateFormat; import java.util.Date; public class LocationActivity extends AppCompatActivity implements AMapLocationListener { //声明aMapLocationClient类对象 public AMapLocationClient mLocationClient = null; //声明AMapLocationClientOption对象 public AMapLocationClientOption mLocationOption = null; private MapView mMapView = null; private AMap aMap; private Marker locationMarker; private LatLng latLng; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_location); //初始化组件 ImageView iv = findViewById(R.id.iv); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (latLng != null) { aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10)); } } }); //获取地图控件引用 mMapView = (MapView) findViewById(R.id.map); //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图 mMapView.onCreate(savedInstanceState); //初始化地图控制器对象 if (aMap == null) { aMap = mMapView.getMap(); } //初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(this); //初始化aMapLocationClientOption对象 mLocationOption = new AMapLocationClientOption(); //设置定位模式为aMapLocationMode.Hight_Accuracy,高精度模式。 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //获取一次定位结果: //该方法默认为false。 mLocationOption.setOnceLocation(true); //获取最近3s内精度最高的一次定位结果: //设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true,setOnceLocation(boolean b) // 接口也会被设置为true,反之不会,默认为false。 mLocationOption.setOnceLocationLatest(true); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //启动定位 mLocationClient.startLocation(); } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 mMapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 mMapView.onSaveInstanceState(outState); } @Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { //定位获取到的用户位置 latLng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()); if (locationMarker == null) { //添加Marker显示定位位置 locationMarker = aMap.addMarker(new MarkerOptions() .position(latLng) .icon(BitmapDescriptorFactory.fromResource(R.drawable.charge_has_net))); } else { //已经添加过了,修改位置即可 locationMarker.setPosition(latLng); } //然后可以移动到定位点,使用animateCamera就有动画效果 aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10)); } else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo()); } } } private void initView(AMapLocation aMapLocation) { //可在其中解析aMapLocation获取相应内容。 aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表 aMapLocation.getLatitude();//获取纬度 aMapLocation.getLongitude();//获取经度 aMapLocation.getAccuracy();//获取精度信息 aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。 aMapLocation.getCountry();//国家信息 aMapLocation.getProvince();//省信息 aMapLocation.getCity();//城市信息 aMapLocation.getDistrict();//城区信息 aMapLocation.getStreet();//街道信息 aMapLocation.getStreetNum();//街道门牌号信息 aMapLocation.getCityCode();//城市编码 aMapLocation.getAdCode();//地区编码 aMapLocation.getAoiName();//获取当前定位点的AOI信息 aMapLocation.getBuildingId();//获取当前室内定位的建筑物Id aMapLocation.getFloor();//获取当前室内定位的楼层 aMapLocation.getGpsAccuracyStatus();//获取GPS的当前状态 //获取定位时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(aMapLocation.getTime()); df.format(date); } }
NaviActivity
package com.example.gaodedemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; 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.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.navi.AMapNavi; import com.amap.api.navi.AMapNaviListener; import com.amap.api.navi.AMapNaviView; import com.amap.api.navi.AMapNaviViewListener; import com.amap.api.navi.enums.NaviType; 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.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.autonavi.tbt.TrafficFacilityInfo; import com.example.gaodedemo.util.TTSController; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class NaviActivity extends AppCompatActivity implements AMapNaviListener, AMapNaviViewListener { private AMapNavi mAMapNavi; private final List<NaviLatLng> sList = new ArrayList<NaviLatLng>(); private final List<NaviLatLng> eList = new ArrayList<NaviLatLng>(); private AMapNaviView mAMapNaviView; protected TTSController mTtsManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navi); mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view); mAMapNaviView.onCreate(savedInstanceState); mAMapNaviView.setAMapNaviViewListener(this); //获取AMapNavi实例 mAMapNavi = AMapNavi.getInstance(getApplicationContext()); //添加监听回调,用于处理算路成功 mAMapNavi.addAMapNaviListener(this); //添加导航声音 mAMapNavi.addAMapNaviListener(mTtsManager); mAMapNavi.setUseInnerVoice(true); //设置模拟导航的行车速度 mAMapNavi.setEmulatorNaviSpeed(75); } @Override protected void onDestroy() { super.onDestroy(); mAMapNavi.stopNavi(); mAMapNavi.destroy(); } @Override public void onInitNaviFailure() { } //当 AMapNavi 对象初始化成功后,会进入 onInitNaviSuccess 回调函数,在该回调函数中调用路径规划方法计算路径。 @Override public void onInitNaviSuccess() { int strategy=0; try { strategy = mAMapNavi.strategyConvert(true, false, false, false, false); } catch (Exception e) { e.printStackTrace(); } sList.clear(); eList.clear(); NaviLatLng mStartLatlng = new NaviLatLng(39.894914, 116.322062); NaviLatLng mEndLatlng = new NaviLatLng(39.903785, 116.423285); sList.add(mStartLatlng); eList.add(mEndLatlng); mAMapNavi.calculateDriveRoute(sList, eList, null, strategy); } @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) { //开始导航 mAMapNavi.startNavi(NaviType.EMULATOR); } @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 onNaviSetting() { } @Override public void onNaviCancel() { } @Override public boolean onNaviBackClick() { return false; } @Override public void onNaviMapMode(int i) { } @Override public void onNaviTurnClick() { } @Override public void onNextRoadClick() { } @Override public void onScanViewButtonClick() { } @Override public void onLockMap(boolean b) { } @Override public void onNaviViewLoaded() { } }
activity_basic_map.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".BasicMapActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btSatellite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="卫星图层"/> <Button android:id="@+id/btNight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="夜间图层"/> <Button android:id="@+id/btTraffic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="路况图层"/> </LinearLayout> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
activity_location.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LocationActivity"> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="15dp" android:background="@drawable/btn_to_my_postion"/> </RelativeLayout>
activity_navi.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".NaviActivity"> <com.amap.api.navi.AMapNaviView android:id="@+id/navi_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </android.support.constraint.ConstraintLayout>AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gaodedemo"> <!-- 允许程序打开网络套接字 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 允许程序设置内置sd卡的写权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 允许程序获取网络状态 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 允许程序访问WiFi网络信息 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 允许程序读写手机状态和身份 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 允许程序访问CellID或WiFi热点来获取粗略的位置 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 用于获取运营商信息,用于支持提供运营商信息相关的接口 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 用于获取wifi的获取权限,wifi信息会用来进行网络定位 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- 用于访问网络,网络定位需要上网 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 用于读取手机当前的状态 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 用于写入缓存数据到扩展存储卡 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 用于申请调用A-GPS模块 --> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <!-- 用于申请获取蓝牙信息进行室内定位 --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:name="com.amap.api.v2.apikey" android:value="71fcd58d5375f6b798468372ce395a90"> //开发者申请的key </meta-data> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BasicMapActivity" /> <activity android:name=".LocationActivity" /> <activity android:name=".NaviActivity"></activity> </application> </manifest>