1. 程式人生 > >android百度地圖實現範圍內標註

android百度地圖實現範圍內標註

在開發的過程中發現一個問題,當你完成一個新的功能時,過一段時間就會淡忘,所以想通過寫部落格的方式來總結記錄下,以後有類似的功能還能參考下,簡單明瞭。
前一陣因為寫公司一個專案,有一個功能是在範圍內進行標註,簡單來說就是在當前位置附近XX米範圍之內點選地圖進行標註,超出範圍則不能進行標註,現在把它抽取出來寫成個demo。

1.效果圖

1.範圍內新增標註:
範圍內新增標註
2.超出範圍新增標註:
超出範圍標註

2.實現思路

首先要獲取當前位置的座標:獲取當前位置使用定位即可;
其次要判斷當前位置是否在範圍內,在範圍內新增標註,超出範圍提示使用者,在百度地圖的計算工具裡面有一個判斷空間關係的方法:
//判斷點pt是否在,以pCenter為中心點,radius為半徑的圓內。
SpatialRelationUtil.isCircleContainsPoint(pCenter, radius, pt);


最後實現點選地圖新增標註:在地圖中新增單擊監聽事件OnMapClickListener,使用百度地圖sdk中的標註覆蓋物即可實現;

3.程式碼

廢話不多說,直接上程式碼:
<1>佈局檔案

<RelativeLayout           xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
tools:context="com.xzx.baidumapdemo.MainActivity" > <com.baidu.mapapi.map.MapView android:id="@+id/bdMapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" /> <LinearLayout android:id="@+id/ll_bigorsmall"
android:layout_width="44dip" android:layout_height="88dip" android:orientation="vertical" android:layout_marginRight="15dip" android:layout_marginBottom="25dip" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:background="@drawable/map_zoom"> <Button android:id="@+id/btn_enlarge" android:layout_width="44dip" android:layout_gravity="center" android:layout_height="44dip" android:background="@null" /> <Button android:id="@+id/btn_narrow" android:layout_width="44dip" android:layout_height="44dip" android:layout_gravity="center" android:background="@null" /> </LinearLayout> <Button android:id="@+id/btn_location" android:layout_width="44dip" android:layout_height="44dip" android:layout_marginBottom="25dip" android:layout_marginLeft="15dip" android:layout_alignParentBottom="true" android:background="@drawable/map_location"/> </RelativeLayout>

<2>主介面

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //在使用SDK各元件之前初始化context資訊,傳入ApplicationContext  
      //注意該方法要再setContentView方法之前實現  
      SDKInitializer.initialize(getApplicationContext()); 
      setContentView(R.layout.activity_main);
      //初始化控制元件
      initView();
      //宣告LocationClient類
      mLocationClient = new LocationClient(
                                  getApplicationContext()); 
      //註冊監聽函式
      mLocationClient.registerLocationListener(myListener);
      //初始化地圖 
      initMap();
      //初始化定位
      initLocation();
      //事件監聽
      setMapListener();
  }
/**
 * 地圖點選事件及標註點選事件
 */
 private void setMapListener() {
     mBaiduMap.setOnMapClickListener(new OnMapClickListener() {  
     /** 
      * 地圖單擊事件回撥函式 
      * @param point 點選的地理座標 
      */  
     public void onMapClick(LatLng point){  
        LatLng pCenter = new LatLng(mLatitude,mLongtitude);
        //判斷點point是否在,以pCenter為中心點,200為半徑的圓內
        boolean isCircle = SpatialRelationUtil
        .isCircleContainsPoint(pCenter, 200, point);
        if(isCircle){
            if(markerA != null){
                markerA.remove();//移除標註
            }
            BitmapDescriptor bitmap = BitmapDescriptorFactory  
                    .fromResource(R.drawable.icon_flag);  
            //構建MarkerOption,用於在地圖上新增Marker  
            OverlayOptions option = new MarkerOptions()  
                .position(point) 
                .icon(bitmap);  
            //在地圖上新增Marker,並顯示  
            markerA = (Marker) mBaiduMap.addOverlay(option);
            lat = point.latitude;
            lng = point.longitude;
        }else {
            toast("超出範圍,無法標註!");
        }
      }  
      /** 
      * 地圖內 Poi 單擊事件回撥函式 
      * @param poi 點選的 poi 資訊 
      */  
      public boolean onMapPoiClick(MapPoi poi){  
        return false;
      }
  });  
   mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
     @Override
     public boolean onMarkerClick(Marker marker) {
         if(marker == markerA){
            marker.remove();
            lat = mLatitude;
            lng = mLongtitude;
         }
        return false;
    }
  });
}
/**
 * 定位監聽
 */
public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 銷燬後不在處理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            //Receive Location
            MyLocationData locData = new MyLocationData.Builder()
                   //定位精度
                   .accuracy(200)
                   // 此處設定開發者獲取到的方向資訊,順時針0-360
                   .direction(100)
                   .latitude(location.getLatitude())
                   .build();
            mBaiduMap.setMyLocationData(locData);
            //更新經緯度
            mLatitude = location.getLatitude();
            mLongtitude = location.getLongitude();
            address = location.getAddrStr();
            if(address.contains("中國")){
                address = address.replace("中國", "");
            }
            if (isFirstLoc) {//是否為首次定位
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatus.Builder builder = new MapStatus
                .Builder();
                builder.target(ll).zoom(17.0f);
                mBaiduMap.animateMapStatus(
                MapStatusUpdateFactory.newMapStatus(
                builder.build()));
            }
        }
    }

注:demo中的地圖縮放等部分功能沒有貼出,這裡貼出的是範圍內標註的主要程式碼。

寫到這裡可能有人會有疑問,你那個藍色的區域就是範圍麼?是的,我直接使用了定位中的定位精度來顯示範圍。預設情況下百度的定位精度為40m,其實兩者沒什麼關係,因專案需要在200m範圍內可以標註,為了使效果更直觀便採取了這種方式。如果大家有更好的方式,歡迎交流!
這是我第一次寫部落格,如果有問題還請大家多多見諒…

相關推薦

android地圖實現範圍標註

在開發的過程中發現一個問題,當你完成一個新的功能時,過一段時間就會淡忘,所以想通過寫部落格的方式來總結記錄下,以後有類似的功能還能參考下,簡單明瞭。 前一陣因為寫公司一個專案,有一個功能是在範圍內進行標註,簡單來說就是在當前位置附近XX米範圍之內點選地圖進行標

Android 地圖如何讓所有的Marker都顯示在螢幕範圍

前言:        高德地圖有方法直接呼叫就可以,總結下百度地圖的(直接複製就可以) 程式碼: LatLngBounds.Builder builder = new LatLngBounds.B

Android 地圖使幾點始終在合適的螢幕範圍顯示

專案中整合百度地圖,需要使幾點的位置在螢幕範圍內顯示,剛開始使用的方放是 MapStatusUpdate mapStatusUpdate1 = MapStatusUpdateFactory.zoomTo(18.0f); mBaiduMap.setMapStatus(mapS

Android 整合地圖實現裝置定位

Android 整合百度地圖實現裝置定位步驟1:申請android 端SDK :http://lbsyun.baidu.com/步驟2:下載基礎版SDK步驟3:下載示例程式步驟4:開始整合:ak加入libs加入SDKInitializer.setCoordType(CoordType.BD09LL);圖示類

android實現手機定位地圖實現

單獨無聊的單機程式並沒有意思,我們何不在程式裡面加上有趣的網路呢, 這次我們主要是講關於Android手機的定位系統,本次使用的是百度定位。   下面表格是提供的兩個我們後面需要的地址 百度定位下載地址 http://lbsyun.baidu.co

Android應用中使用地圖API並新增標註(一)

網上一些資料這種的內容已經過時了,這裡是最新的內容,如果哪裡不對,請吐槽。。。 1)下載百度地圖移動版API(Android)開發包       要在Android應用中使用百度地圖API,就需要在工程中引用百度地圖API開發包,這個開發包包含兩個檔案: 2)申請A

Android 地圖開發(一)如何呼叫地圖介面和在專案中顯示地圖以及實現定位

二、下載百度地圖API庫 然後新增到專案中即可。   三、在專案清單AndroidMainifest.xml配置百度地圖API key和新增相關許可權                         四、在專案呼叫百度地圖專案功能,這篇文章就首先講講顯示地圖和定位的功能 首先

Android定位&地圖&導航——基於地圖實現的定位功能

public class MyApplication extends Application{ public LocationClient mLocationClient = null; public GeofenceClient mGeofenceClient; publi

Android 地圖間接實現Marker點選背景變化效果

最近專案中有需要對百度地圖Marker設定點選效果, 最開始構思使用xml佈局檔案View view = View.inflate(this, R.layout.activity_b, null); B

Android 地圖sdk 標註圖marker中可以切換顯示不同內容

記錄一個前段時間解決的功能需求 先直接上圖片看看實現後的效果: 具體需求為,在地圖頁上顯示出所有的場站marker之後,點選左側的按鈕可以實現動態切換場站marker中顯示的資料。 實現思路為:構造marker時,icon方法中傳入的引數Bit

Android 地圖開發--- 導航功能輸入起始地址實現導航,地址解析與反解析的使用

本人主要介紹安卓開發使用百度地圖實現導航功能,使用者可以輸入當前位置和目的地,實現導航。 首先,我們需要當用戶輸入具體接到地址後將具體街道的地址轉化為經度和緯度,然後實現定位,因此,首先來講解一下地址解析,百度地圖API提供Geocode類進行地址解析,我們可以通過Geo

Android 地圖定位半徑圈範圍自定義

專案需求百度定位的半徑為500米,但是載入下來後的定位半徑為100米(預設大小)。 查詢了好久資料才發現,原來這個所謂的半徑圈表示百度地圖的半徑誤差值大小。 在百度地圖的API中,BDLocationListener,定位的返回監聽事件中設定即可。 locData.dire

[android] 地圖開發 (一).申請AK顯示地圖及解決顯示空白網格問題

定義 gps 官方 應用程序 2.x lns settings sap xmlns 近期做android百度地圖,可是使用baidumapapi_v2_3_1.jar和libBaiduMapSDK_v2_3_1.so顯示百度地圖時總是遇到問題——僅僅顯示

android 地圖(具體請看開放平臺)

int over layout tco pap 平臺 jar pil inter compile files(‘libs/BaiduLBS_Android.jar‘) compile ‘com.android.support:appcompat-v7:26.+‘

使用地圖實現詳細地址自動補全

默認 tro int() div inner 使用 wid type 實現 <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scal

使用地圖實現詳細地址自動補全(補全bug''事件只能綁定到一個上的問題')

item 頁面 tid col border nconf complete ane result     loadMapAutocomplete("suggestId","searchResultPanel"); loadMapAutocomplete("suggest

網頁中利用地圖實現定位省(直轄市)市(區)

ren location .get point asc sca set map char <!doctype html> <head> <meta http-equiv="Content-Type" content="text/h

Android地圖(二)結合方向傳感器我們自己定位哪裏走

troy 真機 pretty 刪除文件 RR tap 大神 素材 near Android百度地圖(二)結合方向傳感器我們自己定位哪裏走 本文代碼在http://b

地圖生成器添加標註不顯示

圖標 技術 路徑 api 發現 不顯示 HR baidu images 最近發現百度地圖生成器生成的地圖中標註的圖片不顯示,如下圖: 很明顯了,就是這個圖標路徑(http://app.baidu.com/map/images/us_mk_icon.png)的問題 在

地圖API 自定義標註圖標

cit rop 設置 src rip ddc 使用 options city 通過Icon類可實現自定義標註的圖標,下面示例通過參數MarkerOptions的icon屬性進行設置, 也可以使用marker.setIcon()方法。 <script type="te