Android百度地圖開發實現點擊地圖添加Marker並擷取地址資訊

來源:互聯網
上載者:User

標籤:

 SDKInitializer.initialize(getApplicationContext());              setContentView(R.layout.activity_map);                            // 擷取地圖控制項引用              mMapView = (MapView) findViewById(R.id.bmapView);              mBaiduMap = mMapView.getMap();              //設定是否顯示比例尺控制項              mMapView.showScaleControl(false);              //設定是否顯示縮放控制項              mMapView.showZoomControls(false);              // 刪除百度地圖LoGo              mMapView.removeViewAt(1);                                          // 設定marker表徵圖              bitmap = BitmapDescriptorFactory.fromResource(R.drawable.maker);              mBaiduMap.setOnMapClickListener(new OnMapClickListener() {                        @Override                  public boolean onMapPoiClick(MapPoi arg0) {                      // TODO Auto-generated method stub                      return false;                  }                        //此方法就是點擊地圖監聽                  @Override                  public void onMapClick(LatLng latLng) {                      //擷取經緯度                      double latitude = latLng.latitude;                      double longitude = latLng.longitude;                      System.out.println("latitude=" + latitude + ",longitude=" + longitude);                      //先清除圖層                      mBaiduMap.clear();                      // 定義Maker座標點                      LatLng point = new LatLng(latitude, longitude);                      // 構建MarkerOption,用於在地圖上添加Marker                      MarkerOptions options = new MarkerOptions().position(point)                              .icon(bitmap);                      // 在地圖上添加Marker,並顯示                      mBaiduMap.addOverlay(options);                      //執行個體化一個地理編碼查詢對象                      GeoCoder geoCoder = GeoCoder.newInstance();                      //設定反地理編碼位置座標                      ReverseGeoCodeOption op = new ReverseGeoCodeOption();                      op.location(latLng);                      //發起反地理編碼請求(經緯度->地址資訊)                      geoCoder.reverseGeoCode(op);                      geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {                                                    @Override                          public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {                              //擷取點擊的座標地址                              address = arg0.getAddress();                              System.out.println("address="+address);  

                  
  /**
* 給地圖上添加文字
*/
//定義文字所顯示的座標點
LatLng llText = new LatLng(latitude, longitude);
//構建文字Option對象,用於在地圖上添加文字
OverlayOptions textOption = new TextOptions()
// .bgColor(0xAAFFFF00)
.fontSize(16)
.fontColor(Color.BLACK)
.text(addrStr)

// .rotate(-30)
.position(llText);
//在地圖上添加該文字對象並顯示

mBaiduMap.addOverlay(textOption);
} @Override public void onGetGeoCodeResult(GeoCodeResult arg0) { } }); } }); } @Override protected void onDestroy() { super.onDestroy(); // 在activity執行onDestroy時執行mMapView.onDestroy(),實現地圖生命週期管理   mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); // 在activity執行onResume時執行mMapView. onResume (),實現地圖生命週期管理   mMapView.onResume(); } @Override protected void onPause() { super.onPause(); // 在activity執行onPause時執行mMapView. onPause (),實現地圖生命週期管理   mMapView.onPause(); } 

 

Android百度地圖開發實現點擊地圖添加Marker並擷取地址資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.