<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
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"
/>
</LinearLayout>
public class wirtepipeFragement extends BaseFragment implements AMapLocationListener, LocationSource {
/**
* 標誌位,標誌已經初始化完成
*/
private boolean isPrepared;
/**
* 是否已被加載過一次,第二次就不再去請求數據了
*/
private boolean mHasLoadedOnce;
private AMap mMap;
private MapView mapView;
private OnLocationChangedListener mListener;
private AMapLocationClient mLocationClient;
private AMapLocationClientOption mLocationOption;
private LatLng myLocation;
private static wirtepipeFragement fragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mView == null) {
// 需要inflate一個佈局文件 填充Fragment
mView = inflater.inflate(R.layout.fragment_writepipe, container, false);
mapView = mView.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
initView();
isPrepared = true;
//實現懶加載
lazyLoad();
}
//緩存的mView需要判斷是否已經被加過parent, 如果有parent需要從parent刪除,要不然會發生這個mView已經有parent的錯誤。
ViewGroup parent = (ViewGroup) mView.getParent();
if (parent != null) {
parent.removeView(mView);
}
return mView;
}
/**
* 初始化控件
*/
private void initView() {
initMap();
setUpLocationStyle();
}
@Override
public void lazyLoad() {
if (!isPrepared || !isVisible || mHasLoadedOnce) {
return;
}
//填充各控件的數據
mHasLoadedOnce = true;
}
public static wirtepipeFragement newInstance() {
if(fragment == null){
synchronized(MapFragment.class){
if(fragment==null){
fragment = new wirtepipeFragement();
}
}
}
return fragment;
}
private void initMap() {
if (mMap == null) {
mMap = mapView.getMap();
}
mMap.setLocationSource(this);// 設置定位監聽
mMap.setMyLocationEnabled(true);
UiSettings uiSettings = mMap.getUiSettings();
uiSettings.setZoomControlsEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(15);
mMap.moveCamera(cameraUpdate);
}
private void setUpLocationStyle() {
// 自定義系統定位藍點
// MyLocationStyle myLocationStyle = new MyLocationStyle();
// myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.gps_point));
// myLocationStyle.strokeWidth(0);
// myLocationStyle.radiusFillColor(Color.TRANSPARENT);
// mMap.setMyLocationStyle(myLocationStyle);
MyLocationStyle myLocationStyle;
myLocationStyle = new MyLocationStyle();//初始化定位藍點樣式類
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
// 連續定位、且將視角移動到地圖中心點,定位點依照設備方向旋轉,並且會跟隨設備移動。(1秒1次定位)如果不設置myLocationType,默認也會執行此種模式。
myLocationStyle.interval(2000); //設置連續定位模式下的定位間隔,只在連續定位模式下生效,單次定位模式下不會生效。單位爲毫秒。
mMap.setMyLocationStyle(myLocationStyle);//設置定位藍點的Style
mMap.getUiSettings().setMyLocationButtonEnabled(true);//設置默認定位按鈕是否顯示,非必需設置。
mMap.setMyLocationEnabled(true);// 設置爲true表示啓動顯示定位藍點,false表示隱藏定位藍點並不進行定位,默認是false。
}
@Override
public void onResume() {
super.onResume();
Log.d("zh", "onResume進來了");
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
Log.d("zh", mapView + ":onPause");
mapView.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("zh", mapView + ":onSaveInstanceState");
mapView.onSaveInstanceState(outState);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("zh", mapView + ":onDestroy進來了");
mLocationOption = null;
mLocationClient = null;
mMap = null;
mapView.onDestroy();
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
Log.d("zh", "onLocationChanged進來了");
Log.d("zh", aMapLocation + "");
Log.d("zh", aMapLocation.getErrorCode() + "");
if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
if (mListener != null) {
mListener.onLocationChanged(aMapLocation);// 顯示系統小藍點
}
//獲取當前經緯度座標
String address = aMapLocation.getAddress();
Log.d("zh",address);
myLocation = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
//fixedMarker();
}
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
Log.d("zh", "activate進來了");
mListener = onLocationChangedListener;
if (mLocationClient == null) {
mLocationClient = new AMapLocationClient(getActivity());
mLocationOption = new AMapLocationClientOption();
//設置定位監聽
mLocationClient.setLocationListener(this);
//設置爲高精度定位模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設置定位參數
mLocationOption.setOnceLocation(true);//只定位一次
mLocationOption.setHttpTimeOut(2000);
mLocationClient.setLocationOption(mLocationOption);
// 此方法爲每隔固定時間會發起一次定位請求,爲了減少電量消耗或網絡流量消耗,
// 注意設置合適的定位時間的間隔(最小間隔支持爲2000ms),並且在合適時間調用stopLocation()方法來取消定位請求
// 在定位結束後,在合適的生命週期調用onDestroy()方法
// 在單次定位情況下,定位無論成功與否,都無需調用stopLocation()方法移除請求,定位sdk內部會移除
mLocationClient.startLocation();//開始定位
}
}
@Override
public void deactivate() {
Log.d("zh", "deactivate我是什麼時候進來的");
mListener = null;
if (mLocationClient != null) {
mLocationClient.stopLocation();
mLocationClient.onDestroy();
}
mLocationClient = null;
}
}