1.下面示例是一個簡單的定位,來自官網,對這些源碼加了一些注釋,這樣看起來可能會更容易了解一點。
2.直接上源碼
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dragon.arnav">
<!--添加對應有權限-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--啟動高德地圖服務,下面的value不要照着輸入,請輸入你申請是的apikey value-->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="fc5d4be7c65a05064dd552395ae46bdb"/>
<!--android:configChanges="orientation|keyboardHidden|screenSize"這句是支援橫豎屏切換,要求系統4.0以上-->
<activity android:name=".Main"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--開啟定位服務要加上這一條,否則會定位失敗,報錯誤碼為10的錯誤-->
<service android:name="com.amap.api.location.APSService" >
</service>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dragon.arnav.Main">
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.amap.api.maps.MapView>
<TextView
android:id="@+id/location_errInfo_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:background="@color/red"
android:textColor="@color/darkgrey"
android:text="TextView"
android:visibility="gone"/>
<RadioGroup
android:id="@+id/gps_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@color/grey"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/gps_locate_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/gpslocate"
android:textColor="@android:color/black" />
<RadioButton
android:id="@+id/gps_follow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gpsfollow"
android:textColor="@android:color/black" />
<RadioButton
android:id="@+id/gps_rotate_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gpsrotate"
android:textColor="@android:color/black" />
</RadioGroup>
</FrameLayout>
main.java
package com.dragon.arnav;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
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.LocationSource;
import com.amap.api.maps.MapView;
//添加定位接口
public class Main extends Activity implements LocationSource,
AMapLocationListener,OnCheckedChangeListener {
private AMap aMap;
private MapView mapView;
private OnLocationChangedListener mListener;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
private RadioGroup mGPSModeGroup;
private TextView mLocationErrText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 不顯示程式的标題欄
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 此方法必須重寫
init();
}
/**
* 初始化
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
setUpMap();
}
mGPSModeGroup = (RadioGroup) findViewById(R.id.gps_radio_group);
mGPSModeGroup.setOnCheckedChangeListener(this);
mLocationErrText = (TextView)findViewById(R.id.location_errInfo_text);
mLocationErrText.setVisibility(View.GONE);
}
/**
* 設定一些amap的屬性
*/
private void setUpMap() {
aMap.setLocationSource(this);// 設定定位監聽
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設定預設定位按鈕是否顯示
aMap.setMyLocationEnabled(true);// 設定為true表示顯示定位層并可觸發定位,false表示隐藏定位層并不可觸發定位,預設是false
// 設定定位的類型為定位模式 ,可以由定位、跟随或地圖根據面向方向旋轉幾種
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);//定位模式
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.gps_locate_button:
// 設定定位的類型為定位模式
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
break;
case R.id.gps_follow_button:
// 設定定位的類型為 跟随模式
aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
break;
case R.id.gps_rotate_button:
// 設定定位的類型為根據地圖面向方向旋轉
aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);
break;
}
}
/**
* 方法必須重寫
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必須重寫
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
deactivate();
}
/**
* 方法必須重寫
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必須重寫
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
if(null != mlocationClient){
mlocationClient.onDestroy();
}
}
/**
* 定位成功後回調函數
*/
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (mListener != null && amapLocation != null) {
if (amapLocation != null
&& amapLocation.getErrorCode() == ) {
mLocationErrText.setVisibility(View.GONE);
mListener.onLocationChanged(amapLocation);// 顯示系統小藍點
} else {
String errText = "定位失敗," + amapLocation.getErrorCode()+ ": " + amapLocation.getErrorInfo();
Log.e("AmapErr",errText);
mLocationErrText.setVisibility(View.VISIBLE);
mLocationErrText.setText(errText);
}
}
}
/**
* 激活定位
*/
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
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();
}
}
/**
* 停止定位
*/
@Override
public void deactivate() {
mListener = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;
}
}
3.還有一些檔案,大家可以自己采用通用的就好如strings.xml,colors.xml,大家用用通用的就好,這裡就不提供了。
4.效果圖
哈哈,發現你在哪了,呵呵,你是對的。
歡迎大家繼續關注後續更新,有從事AR/VR研究的朋友可以加入下面QQ群或是關注下面的微信公衆号。