2014-12-05 76 views
0

餘米開發一個應用程序中,用戶繪製多邊形。我有一套LatLng。現在我想顯示屬於LatLng集合的所有標記。所有不屬於我的標記我想刪除它。我附上了一張圖片。我怎麼能做到這一點。提前致謝。如何獲取區域從一組經緯度的谷歌地圖

static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
    static final LatLng cz1 = new LatLng(53.600, 9.927); 
    static final LatLng KIEL = new LatLng(53.551, 9.993); 
    private GoogleMap map; 
    private CameraPosition cameraPosition; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) 
       .title("Hamburg")); 

     Marker cz = map.addMarker(new MarkerOptions().position(cz1) 
       .title("cz")); 
     Marker kiel = map.addMarker(new MarkerOptions() 
       .position(KIEL) 
       .title("Kiel") 
       .snippet("Kiel is cool") 
       .icon(BitmapDescriptorFactory 
         .fromResource(R.drawable.ic_launcher))); 

     // Move the camera instantly to hamburg with a zoom of 15. 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); 

     Button button = (Button) findViewById(R.id.map_button); 
     button.setOnClickListener(this); 
     // Zoom in, animating the camera. 
     map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
     map.setOnCameraChangeListener(this); 
    } 

    @Override 
    public void onCameraChange(CameraPosition arg0) { 
     cameraPosition = arg0; 
    } 

    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(MainActivity.this, FreeDrawActivity.class); 
     Bundle bundle = new Bundle(); 
     bundle.putParcelable("position", cameraPosition); 
     intent.putExtras(bundle); 
     startActivityForResult(intent, 0); 
    } 

    @Override 
    protected void onActivityResult(int arg0, int arg1, Intent arg2) { 
     super.onActivityResult(arg0, arg1, arg2); 
     if (arg2 != null) { 
      Draw_Map((ArrayList<LatLng>) arg2.getSerializableExtra("result")); 
     } 

    } 

    public void Draw_Map(ArrayList<LatLng> val) { 
     PolygonOptions rectOptions = new PolygonOptions(); 
     rectOptions.addAll(val); 
     rectOptions.strokeColor(Color.BLUE); 
     rectOptions.strokeWidth(7); 
     rectOptions.fillColor(Color.argb(125, 255, 255, 255)); 
     Polygon polygon = map.addPolygon(rectOptions); 
    } 

回答

0

要請檢查是否指定LatLng屬於一組經緯度的。然後,您需要使用由Google開發人員提供的PolyUtil庫Download from here。並使用

PolyUtil.containsLocation(LatLng Object, set of lat lng, boolean) 

如果給定的latlng屬於一組latlng,則此方法返回true。其他明智假

爲前: -

if(PolyUtil.containsLocation(KIEL, val, false)) { 
      kiel = map.addMarker(new MarkerOptions() 
      .position(KIEL) 
      .title("Kiel") 
      .snippet("Kiel is cool") 
      .icon(BitmapDescriptorFactory 
        .fromResource(R.drawable.ic_launcher))); 
     } 
0

如果我理解你的問題吧,你要確定一個點(即「標記」)的內部或由用戶繪製一個給定的多邊形之外。在這種情況下,你需要實現這個算法http://en.wikipedia.org/wiki/Point_in_polygon

+0

我想要顯示的多邊形繪圖內部的所有點。 – 2014-12-05 11:47:44

+0

該算法將允許您過濾多邊形內的哪些點。然後畫這些。 – Moses 2014-12-05 12:13:42