高德地圖定位

對高德地圖的定位進行封裝,方便調用android

以下是高德官網的指導:http://lbs.amap.com/api/android-sdk/guide/create-map/mylocationgit

以下是主要的代碼  定位使用的是(5.0.0版本後的),主要記錄下封裝的思路api

  1 public class MapLbsLayerImpl implements ILbsLayer {
  2 
  3     private Context mContext;
  4     private MapView mapView; // 地圖視圖對象
  5     private AMap aMap; // 地圖管理對象
  6     private AMapLocationClient mLocationClient; // 申明對象
  7     private AMapLocationClientOption mLocationOption = null; // 聲明LocationOption對象
  8     private LocationSource.OnLocationChangedListener mListener;
  9     private CommonLocationChangeListener mLocationChangeListener;
 10 
 11     public MapLbsLayerImpl(Context context) {
 12         this.mContext = context;
 13         mapView = new MapView(mContext);
 14         if (aMap == null) aMap = mapView.getMap();
 15         // 隱藏高德地圖右下角縮放的按鈕
 16         aMap.getUiSettings().setZoomControlsEnabled(false);
 17         aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
 18             @Override
 19             public boolean onMarkerClick(Marker marker) {
 20                 return true;
 21             }
 22         });
 23 
 24     }
 25 
 26     /**
 27      * 地圖控件
 28      *
 29      * @return View
 30      */
 31     @Override
 32     public View getMapView() {
 33         return mapView;
 34     }
 35 
 36     /**
 37      * 開始定位
 38      */
 39     @Override
 40     public void getStartLocationMap() {
 41         // 設置定位監聽
 42         aMap.setLocationSource(new LocationSource() {
 43             /**
 44              * 激活定位
 45              * @param onLocationChangedListener OnLocationChangedListener
 46              */
 47             @Override
 48             public void activate(OnLocationChangedListener onLocationChangedListener) {
 49                 mListener = onLocationChangedListener;
 50                 if (mLocationClient == null) {
 51                     // 初始化定位
 52                     mLocationClient = new AMapLocationClient(mContext);
 53                     // 初始化定位參數
 54                     mLocationOption = new AMapLocationClientOption();
 55                     // 設置定位回調監聽
 56                     mLocationClient.setLocationListener(locationListener);
 57                     // 設置定位模式
 58                     if (ToastUtils.isOpGPS()) {
 59                         // 設置爲高精度定位模式
 60                         mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
 61                     } else {
 62                         // 設置爲低精度定位模式
 63                         mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
 64                     }
 65                     mLocationOption.setInterval(6000); // 設置多久定位一次
 66                     // mLocationOption.setOnceLocation(true);
 67                     // 設置定位參數
 68                     mLocationClient.setLocationOption(mLocationOption);
 69                     mLocationClient.startLocation(); // 啓動定位
 70                 }
 71             }
 72 
 73             /**
 74              * 中止定位
 75              */
 76             @Override
 77             public void deactivate() {
 78                 mListener = null;
 79                 if (mLocationClient != null) {
 80                     mLocationClient.stopLocation();
 81                     mLocationClient.onDestroy();
 82                 }
 83                 mLocationClient = null;
 84             }
 85         });
 86         // 設置爲true表示顯示定位層並可觸發定位,false表示隱藏定位層並不可觸發定位,默認是false
 87         aMap.setMyLocationEnabled(true);
 88     }
 89 
 90     /**
 91      * 定位的監聽
 92      */
 93     private boolean isFirst = true;
 94     private AMapLocationListener locationListener = new AMapLocationListener() {
 95         @Override
 96         public void onLocationChanged(AMapLocation aMapLocation) {
 97             if (mListener != null && aMapLocation != null) {
 98                 if (aMapLocation.getErrorCode() == 0) {
 99                     Log.d("jiejie", "---------定位成功-------");
100                     LocationInfo info = new LocationInfo(aMapLocation.getLatitude(),
101                             aMapLocation.getLongitude());
102                     info.setKey("0000");
103                     if (isFirst) {
104                         isFirst = false;
105                         moveCameraToPoint(info, 15);
106                         if (mLocationChangeListener != null) {
107                             mLocationChangeListener.onLocation(info);
108                         }
109                     }
110                     if (mLocationChangeListener != null) {
111                         mLocationChangeListener.onLocationChanged(info);
112                     }
113                 } else {
114                     Log.d("jiejie", "定位失敗" + aMapLocation.getErrorCode() + " " + aMapLocation.getErrorInfo());
115                 }
116             }
117         }
118     };
119     private Map<String, Marker> markerMap = new HashMap<>(); // 管理地圖標記的集合
120 
121     /**
122      * 添加Marker
123      *
124      * @param locationInfo 位置信息
125      * @param bitmap       圖片
126      * @param isLoad       是否展現動畫
127      */
128     @Override
129     public void addOnUpdateMarker(LocationInfo locationInfo, Bitmap bitmap, boolean isLoad) {
130         Marker storeMarker = markerMap.get(locationInfo.getKey());
131         LatLng latLng = new LatLng(locationInfo.getLatitude(), locationInfo.getLongitude());
132         if (storeMarker != null) {
133             // 若是已經存在則更新
134             storeMarker.setPosition(latLng);
135         } else {
136             // 若是沒有則建立
137             MarkerOptions options = new MarkerOptions();
138             BitmapDescriptor des = BitmapDescriptorFactory.fromBitmap(bitmap);
139             options.icon(des);
140             options.anchor(0.5f, 0.5f);
141             options.position(latLng);
142             Marker marker = aMap.addMarker(options);
143             if (isLoad) startGrowAnimation(marker);
144             markerMap.put(locationInfo.getKey(), marker);
145         }
146     }
147 
148     /**
149      * 地上生長得Marker
150      *
151      * @param marker Marker
152      */
153     private void startGrowAnimation(Marker marker) {
154         if (marker != null) {
155             Animation animation = new ScaleAnimation(0, 1, 0, 1);
156             animation.setInterpolator(new LinearInterpolator());
157             // 整個移動所須要的時間
158             animation.setDuration(600);
159             // 設置動畫
160             marker.setAnimation(animation);
161             // 開始動畫
162             marker.startAnimation();
163         }
164     }
165 
166     /**
167      * 位置改變
168      *
169      * @param changeListener Change
170      */
171     @Override
172     public void setLocationChangeListener(CommonLocationChangeListener changeListener) {
173         this.mLocationChangeListener = changeListener;
174     }
175 
176     /**
177      * 移動地圖中心到某個點
178      *
179      * @param locationInfo 地址
180      * @param scale        縮放係數
181      */
182     @Override
183     public void moveCameraToPoint(LocationInfo locationInfo, int scale) {
184         LatLng latLng = new LatLng(locationInfo.getLatitude(), locationInfo.getLongitude());
185         CameraUpdate update = CameraUpdateFactory.newCameraPosition(
186                 new CameraPosition(latLng, scale, 0, 0)
187         );
188         aMap.moveCamera(update);
189     }
190 
191 
192     @Override
193     public void onCreate(Bundle bundle) {
194         mapView.onCreate(bundle);
195     }
196 
197     @Override
198     public void onResume() {
199         mapView.onResume();
200     }
201 
202     @Override
203     public void onPause() {
204         mapView.onPause();
205     }
206 
207     @Override
208     public void onDestroy() {
209         mapView.onDestroy();
210         if (null != mLocationClient) {
211             mLocationClient.onDestroy();
212             mLocationClient = null;
213         }
214     }

這樣在UI界面就能夠更簡便的調用了ide

    private void initView() {
        // 開始定位
        mLbsLayer.getStartLocationMap();
        // 定位返回的監聽
        mLbsLayer.setLocationChangeListener(new ILbsLayer.CommonLocationChangeListener() {
            @Override
            public void onLocationChanged(LocationInfo locationInfo) { // 位置改變
                Log.d("jiejie","--main---------");
            }

            @Override
            public void onLocation(LocationInfo locationInfo) { // 第一次定位
                if (mLocationBit == null || mLocationBit.isRecycled()) {
                    mLocationBit = BitmapFactory.decodeResource(getResources(), R.drawable.navi_map_gps_locked);
                }
                mLbsLayer.addOnUpdateMarker(locationInfo, mLocationBit, false); // 第一次定位添加Marker,該Marker不設置生長動畫
            }
        });
    }
相關文章
相關標籤/搜索