參考文檔:
https://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation
https://lbs.amap.com/api/android-sdk/guide/draw-on-map/draw-marker
啓動定位服務器
//啓動定位服務器
protected void doLocation()
{
//1 創建一個客戶端定位句柄
_amapLocationClient = new AMapLocationClient(getApplicationContext());
//2 給客戶端句柄設置一個listenner來處理服務器返回的定位數據
_amapLocationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
//onLocationChanged 就是如果服務器給客戶端返回數據,調用的回調函數
//aMapLocation 就是服務器給客戶端返回的定位數據
if (aMapLocation != null) {
//服務器是有響應的
if(aMapLocation.getErrorCode() == 0) {
//定位成功,aMapLocation獲取數據
Log.e("Amap", "location succ address = "+ aMapLocation.getAddress());
Log.e("Amap", "city = "+ aMapLocation.getCity());
Log.e("Amap", "longtitude = " + aMapLocation.getLongitude());
Log.e("Amap", "latitude = " + aMapLocation.getLatitude());
}
else {
//定位失敗,
Log.e("Amap", "location error, code = "+ aMapLocation.getErrorCode()+
", info = "+ aMapLocation.getErrorInfo());
}
}
}
});
//3 開啓定位服務
_amapLocationClient.startLocation();
}
向固定的經緯度添加一個標記,添加一個位置的圖片
//向固定的經緯度添加一個標記
protected void addMarkerToMap(double latitude, double longitude)
{
_selfMarker = _amap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude))
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_marker))));
}
以某個經緯度爲中心來展示地圖
//以某個經緯度爲中心來展示地圖
protected void moveMap(double latitude, double longtiude)
{
LatLng lagLng = new LatLng(latitude, longtiude);
//移動amap地圖 以之前的縮放比例展示
_amap.animateCamera(CameraUpdateFactory.newLatLngZoom(lagLng, _amap.getCameraPosition().zoom));
}
獲取服務器返回的定位數據
//2 給客戶端句柄設置一個listenner來處理服務器返回的定位數據
_amapLocationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
//onLocationChanged 就是如果服務器給客戶端返回數據,調用的回調函數
//aMapLocation 就是服務器給客戶端返回的定位數據
if (aMapLocation != null) {
//服務器是有響應的
if(aMapLocation.getErrorCode() == 0) {
//定位成功,aMapLocation獲取數據
Log.e("Amap", "location succ address = "+ aMapLocation.getAddress());
Log.e("Amap", "city = "+ aMapLocation.getCity());
Log.e("Amap", "longtitude = " + aMapLocation.getLongitude());
Log.e("Amap", "latitude = " + aMapLocation.getLatitude());
}
else {
//定位失敗,
Log.e("Amap", "location error, code = "+ aMapLocation.getErrorCode()+
", info = "+ aMapLocation.getErrorInfo());
}
}
}
});
DriverActivity.java
package com.ldw.hello;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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.AMapOptions;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
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;
public class DriverActivity extends AppCompatActivity {
//創建一個地圖容器MapView對象
private MapView _mapView = null;
//地圖的UISetting對象 給amap設置地圖內嵌控件
private UiSettings _uiSettings = null;
//地圖對象
private AMap _amap = null;
//定位服務器客戶端句柄
private AMapLocationClient _amapLocationClient = null;
//定位服務器客戶端句柄屬性
private AMapLocationClientOption _amapLocationOption = null;
//顯示自我位置的圖標
private Marker _selfMarker = null;
boolean isAddSelfMarker = false;
int flag = 0;
int traffic_flag = 0;
protected void initUI() {
//將地圖容器跟MapView控件相關聯
_mapView = (MapView) findViewById(R.id.DriverMap);
}
protected void createMap(Bundle savedInstanceState) {
//展示地圖容器
_mapView.onCreate(savedInstanceState);
//得到amap對象
_amap = _mapView.getMap();
//默認顯示實時交通信息
_amap.setTrafficEnabled(true);
//得到UISettings
_uiSettings = _amap.getUiSettings();
//添加一個指南針控件
_uiSettings.setCompassEnabled(true);
//添加一個縮放比例尺
_uiSettings.setScaleControlsEnabled(true);
//修改logo位置
_uiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);
}
//以某個經緯度爲中心來展示地圖
protected void moveMap(double latitude, double longtiude)
{
LatLng lagLng = new LatLng(latitude, longtiude);
//移動amap地圖 以之前的縮放比例展示
_amap.animateCamera(CameraUpdateFactory.newLatLngZoom(lagLng, _amap.getCameraPosition().zoom));
}
//向固定的經緯度添加一個標記
protected void addMarkerToMap(double latitude, double longitude)
{
_selfMarker = _amap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude))
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_marker))));
}
//啓動定位服務器
protected void doLocation()
{
//1 創建一個客戶端定位句柄
_amapLocationClient = new AMapLocationClient(getApplicationContext());
//1.5 給定位客戶端設置一些屬性
_amapLocationOption = new AMapLocationClientOption();
//每個5s定位一次
_amapLocationOption.setInterval(3000);
//_amapLocationOption.setOnceLocation(true);
//將option設置給client對象
_amapLocationClient.setLocationOption(_amapLocationOption);
//2 給客戶端句柄設置一個listenner來處理服務器返回的定位數據
_amapLocationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
//onLocationChanged 就是如果服務器給客戶端返回數據,調用的回調函數
//aMapLocation 就是服務器給客戶端返回的定位數據
if (aMapLocation != null) {
//服務器是有響應的
if(aMapLocation.getErrorCode() == 0) {
//定位成功,aMapLocation獲取數據
Log.e("Amap", "location succ address = "+ aMapLocation.getAddress());
Log.e("Amap", "city = "+ aMapLocation.getCity());
Log.e("Amap", "longtitude = " + aMapLocation.getLongitude());
Log.e("Amap", "latitude = " + aMapLocation.getLatitude());
if (isAddSelfMarker == false) {
//在此位置添加一個標記
addMarkerToMap(aMapLocation.getLatitude(), aMapLocation.getLongitude());
isAddSelfMarker = true;
//以自我爲中心展示地圖
moveMap(aMapLocation.getLatitude(), aMapLocation.getLongitude());
}
}
else {
//定位失敗,
Log.e("Amap", "location error, code = "+ aMapLocation.getErrorCode()+
", info = "+ aMapLocation.getErrorInfo());
}
}
}
});
//3 開啓定位服務
_amapLocationClient.startLocation();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver);
initUI();
createMap(savedInstanceState);
doLocation();
}
@Override
protected void onDestroy() {
super.onDestroy();
_mapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
_mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
_mapView.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
_mapView.onSaveInstanceState(outState);
}
}