高德地图的集成步骤
先到高德開放平臺下SDk
將jnLibs文件夾整個都復制到main下
導入依賴
加入權限
下面是代碼
MainActivity
public class MainActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {MapView mMapView = null;//初始化地圖控制器對象AMap aMap;//定位需要的數據LocationSource.OnLocationChangedListener mListener;AMapLocationClient mlocationClient;AMapLocationClientOption mLocationOption;//定位藍點MyLocationStyle myLocationStyle;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//獲取地圖控件引用mMapView = (MapView) findViewById(R.id.map);//在activity執行onCreate時執行mMapView.onCreate(savedInstanceState),創建地圖mMapView.onCreate(savedInstanceState);if (aMap == null) {aMap = mMapView.getMap();}//設置地圖的放縮級別aMap.moveCamera(CameraUpdateFactory.zoomTo(12));// 設置定位監聽aMap.setLocationSource(this);// 設置為true表示顯示定位層并可觸發定位,false表示隱藏定位層并不可觸發定位,默認是falseaMap.setMyLocationEnabled(true);// 設置定位的類型為定位模式,有定位、跟隨或地圖根據面向方向旋轉幾種aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);//藍點初始化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。myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//連續定位、且將視角移動到地圖中心點,定位點依照設備方向旋轉,并且會跟隨設備移動。(1秒1次定位)默認執行此種模式。myLocationStyle.showMyLocation(true);aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {@Overridepublic void onMyLocationChange(Location location) {//從location對象中獲取經緯度信息,地址描述信息,建議拿到位置之后調用逆地理編碼接口獲取}});}@Overrideprotected void onResume() {super.onResume();//在activity執行onResume時執行mMapView.onResume (),重新繪制加載地圖mMapView.onResume();}@Overrideprotected void onPause() {super.onPause();//在activity執行onPause時執行mMapView.onPause (),暫停地圖的繪制mMapView.onPause();}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);//在activity執行onSaveInstanceState時執行mMapView.onSaveInstanceState (outState),保存地圖當前的狀態mMapView.onSaveInstanceState(outState);}//--定位監聽---------/*** 激活定位*/@Overridepublic void activate(OnLocationChangedListener onLocationChangedListener) {mListener = onLocationChangedListener;if (mlocationClient == null) {//初始化定位mlocationClient = new AMapLocationClient(this);//初始化定位參數mLocationOption = new AMapLocationClientOption();//設置定位回調監聽mlocationClient.setLocationListener(this);//設置為高精度定位模式mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//設置定位參數mlocationClient.setLocationOption(mLocationOption);// 此方法為每隔固定時間會發起一次定位請求,為了減少電量消耗或網絡流量消耗,// 注意設置合適的定位時間的間隔(最小間隔支持為2000ms),并且在合適時間調用stopLocation()方法來取消定位請求// 在定位結束后,在合適的生命周期調用onDestroy()方法// 在單次定位情況下,定位無論成功與否,都無需調用stopLocation()方法移除請求,定位sdk內部會移除mlocationClient.startLocation();//啟動定位}}/*** 停止定位*/@Overridepublic void deactivate() {mListener = null;if (mlocationClient != null) {mlocationClient.stopLocation();mlocationClient.onDestroy();}mlocationClient = null;}//定位回調 在回調方法中調用“mListener.onLocationChanged(amapLocation);”可以在地圖上顯示系統小藍點。@Overridepublic void onLocationChanged(AMapLocation aMapLocation) {if (mListener != null && aMapLocation != null) {if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {mListener.onLocationChanged(aMapLocation);// 顯示系統小藍點} else {String errText = "定位失敗," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();Log.e("定位AmapErr", errText);}}}@Overrideprotected void onDestroy() {super.onDestroy();//在activity執行onDestroy時執行mMapView.onDestroy(),銷毀地圖mMapView.onDestroy();if (null != mlocationClient) {mlocationClient.onDestroy();}} }activity_main
<?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="com.example.piomap.MainActivity"><com.amap.api.maps.MapView android:id="@+id/map"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>顯示小藍點可以根據自己想要的去做更改詳情可以看官方的文檔:http://lbs.amap.com/api/android-sdk/guide/create-map/mylocation總結
- 上一篇: 分享缩小视频,虚化边框的方法
- 下一篇: 百度Apollo 2.5 导航模式 使