2016-06-09 53 views
3

我使用地圖顯示了我的應用程序。 在這張地圖中,我在設備的當前位置放置了一個標記。我也圍繞標記添加了一圈如下:Android - 在地圖中僅顯示包含在確定區域中的標記

Circle circle = mMap.addCircle(new CircleOptions() 
         .center(latLng) 
         .radius(400)  //The radius of the circle, specified in meters. It should be zero or greater. 
         .strokeColor(Color.rgb(0, 136, 255)) 
         .fillColor(Color.argb(20, 0, 136, 255))); 

結果是這樣的:
here's an example of the result

我有一個數據庫,其中包含一些以緯度和經度爲特徵的職位。

我會在地圖中設置標記,僅限位於先前添加的圓圈內的位置。
我怎樣才能瞭解他們哪一個被包含在那個區域?

請幫助我,謝謝!

回答

5

您可以添加所有標記,使它們不可見,然後計算您的圓圈中心和標記之間的距離,使可見位於給定距離內的標記:

private List<Marker> markers = new ArrayList<>(); 

// ... 

private void drawMap(LatLng latLng, List<LatLng> positions) { 
    for (LatLng position : positions) { 
     Marker marker = mMap.addMarker(
       new MarkerOptions() 
         .position(position) 
         .visible(false)); // Invisible for now 
     markers.add(marker); 
    } 

    //Draw your circle 
    Circle circle = mMap.addCircle(new CircleOptions() 
      .center(latLng) 
      .radius(400) 
      .strokeColor(Color.rgb(0, 136, 255)) 
      .fillColor(Color.argb(20, 0, 136, 255))); 

    for (Marker marker : markers) { 
     if (SphericalUtil.computeDistanceBetween(latLng, marker.getPosition()) < 400) { 
      marker.setVisible(true); 
     } 
    } 
} 

請注意,我使用的方法從Google Maps API Utility Library

相關問題