(四)高德地圖之定位的幾種模式
阿新 • • 發佈:2018-11-26
這一節主要實現的功能是地圖定位的幾種模式,包括展示、定位、追隨、旋轉、旋轉位置、跟隨不移動中心點、旋轉不移動中心點、旋轉位置不移動到中心點,我們根據實際需要來選擇用那種模式。下面還是主要從程式碼中來體現,主要部分有註釋。
還是先新建佈局檔案:activity_locationmodesource.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" > </com.amap.api.maps.MapView> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/btn_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="展示"/> <Button android:id="@+id/btn_locate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定位"/> <Button android:id="@+id/btn_follow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="追隨"/> <Button android:id="@+id/btn_rotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉"/> <Button android:id="@+id/btn_rotate_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉位置"/> <Button android:id="@+id/btn_follow_nocenter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跟隨不移動中心點"/> <Button android:id="@+id/btn_rotate_nocenter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉不移動到中心點"/> <Button android:id="@+id/btn_rotate_location_nocenter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉位置不移動到中心點"/> </LinearLayout> </RelativeLayout>
然後建立類檔案LocationModeSource.java
package com.junto.gdmaptest.activity; import android.app.Activity; import android.location.Location; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.Log; 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; import com.junto.gdmaptest.R; /** * Created by WangJinyong on 2018/10/24. * 定位的幾種模式 */ public class LocationModeSourceActivity extends Activity implements View.OnClickListener,AMap.OnMyLocationChangeListener { MapView mapView; AMap aMap; Button btn_show,btn_locate,btn_follow,btn_rotate,btn_rotate_location,btn_follow_nocenter,btn_rotate_nocenter,btn_rotate_location_nocenter; private MyLocationStyle myLocationStyle; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activiy_locationmodesource); mapView = findViewById(R.id.map); mapView.onCreate(savedInstanceState); initView(); } @Override protected void onResume() { super.onResume(); mapView.onResume(); } @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } private void initView(){ if (aMap == null) { aMap = mapView.getMap(); setUpMap(); } btn_show = findViewById(R.id.btn_show); btn_show.setOnClickListener(this); btn_locate = findViewById(R.id.btn_locate); btn_locate.setOnClickListener(this); btn_follow = findViewById(R.id.btn_follow); btn_follow.setOnClickListener(this); btn_rotate = findViewById(R.id.btn_rotate); btn_rotate.setOnClickListener(this); btn_rotate_location = findViewById(R.id.btn_rotate_location); btn_rotate_location.setOnClickListener(this); btn_follow_nocenter = findViewById(R.id.btn_follow_nocenter); btn_follow_nocenter.setOnClickListener(this); btn_rotate_nocenter = findViewById(R.id.btn_rotate_nocenter); btn_rotate_nocenter.setOnClickListener(this); btn_rotate_location_nocenter = findViewById(R.id.btn_rotate_location_nocenter); btn_rotate_location_nocenter.setOnClickListener(this); //設定SDK 自帶定位訊息監聽 aMap.setOnMyLocationChangeListener(this); } /** * 設定一些amap的屬性 */ private void setUpMap() { // 如果要設定定位的預設狀態,可以在此處進行設定 myLocationStyle = new MyLocationStyle(); aMap.setMyLocationStyle(myLocationStyle); aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設定預設定位按鈕是否顯示 aMap.setMyLocationEnabled(true);// 設定為true表示顯示定位層並可觸發定位,false表示隱藏定位層並不可觸發定位,預設是false } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_show://展示 // 只定位,不進行其他操作 aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW)); break; case R.id.btn_locate://定位 aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE)); break; case R.id.btn_follow://追隨 aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW)); break; case R.id.btn_rotate://旋轉 aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE)); break; case R.id.btn_rotate_location://旋轉位置 aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE)); break; case R.id.btn_follow_nocenter://跟隨比移動中心點 aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER)); break; case R.id.btn_rotate_nocenter://旋轉不移動到中心點 aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE_NO_CENTER)); break; case R.id.btn_rotate_location_nocenter://旋轉位置不移動到中心點 aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER)); break; } } @Override public void onMyLocationChange(Location location) { // 定位回撥監聽 if (location != null){ Log.e("amap", "onMyLocationChange 定位成功, lat: " + location.getLatitude() + " lon: " + location.getLongitude()); Bundle bundle = location.getExtras(); if (bundle != null){ int errorCode = bundle.getInt(MyLocationStyle.ERROR_CODE); String errorInfo = bundle.getString(MyLocationStyle.ERROR_INFO); // 定位型別,可能為GPS WIFI等,具體可以參考官網的定位SDK介紹 int locationType = bundle.getInt(MyLocationStyle.LOCATION_TYPE); Log.e("amap", "定位資訊, code: " + errorCode + " errorInfo: " + errorInfo + " locationType: " + locationType ); }else { Log.e("amap", "定位資訊, bundle is null "); } }else { Log.e("amap", "定位失敗"); } } }
上面就是實現地圖定位幾種模式的全部內容,根據需要來選擇使用吧。