(五)高德地圖之新增groundoverlay覆蓋物
阿新 • • 發佈:2018-11-26
本節主要實現的功能是往地圖上新增一個groundoverlay覆蓋物,用此方式可以實現公園電子導遊,下面先來看一張效果圖吧:
接下來我們還是直接上程式碼:
新建佈局檔案activity_groundoverlay.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.amap.api.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
新建類檔案:GroundOverlayActivity.java
package com.junto.gdmaptest.activity; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.GroundOverlayOptions; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.LatLngBounds; import com.junto.gdmaptest.R; /** * Created by WangJinyong on 2018/10/26. * 往地圖上新增一個groundoverlay覆蓋物 */ public class GroundOverlayActivity extends Activity { private MapView mapView; private AMap aMap; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_groundoverlay); mapView = findViewById(R.id.mapView); 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(); addOverLayToMap(); } aMap.showBuildings(false); LatLngBounds latLngBounds = new LatLngBounds(new LatLng(39.935029, 116.384377), new LatLng(39.939577, 116.388331)); aMap.setMapStatusLimits(latLngBounds); } //往地圖上新增一個groundoverlay覆蓋物 private void addOverLayToMap(){ aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.936713, 116.386475),18)); LatLngBounds bounds = new LatLngBounds.Builder()//設定顯示在螢幕中的地圖地理範圍,地圖中點顯示為兩個點的中點 .include(new LatLng(39.935029, 116.384377)) .include(new LatLng(39.939577, 116.388331)).build(); aMap.addGroundOverlay(new GroundOverlayOptions() .anchor(10.0f, 10.0f)//設定ground覆蓋物的錨點比例,預設為0.5f,水平和垂直方向都居中對齊 .transparency(0.0f)//設定覆蓋物的透明度,範圍:0.0~1.0 .zIndex(0)//設定覆蓋物的層次,zIndex值越大越在上層; .image(BitmapDescriptorFactory.fromResource(R.mipmap.groundoverlay))//覆蓋物圖片 .positionFromBounds(bounds)); } }
在這裡順便把覆蓋物的圖片也附上一張,這個圖片需要根據實際需求設計來做的,在這裡只是為了方便做demo來實現類似效果
本節到這裡又結束啦,是不很簡單