2013-01-15 20 views
0

谷歌地圖API V2實現多邊形我實現了Android手機上的谷歌地圖API V2,我得到了地圖顯示,並且還多邊形來使用此代碼顯示:與Android中

public class MainActivity extends FragmentActivity { 
Polygon polygon; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

GoogleMap googleMap; 
googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap(); 
LatLng latLng = new LatLng(35.20418,-90.08342); 
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
googleMap.addMarker(new MarkerOptions() 
     .position(latLng) 
     .title("My Spot") 
     .snippet("This is my spot!") 
     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 
googleMap.getUiSettings().setCompassEnabled(true); 
googleMap.getUiSettings().setZoomControlsEnabled(true); 
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10)); 

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 
if(status == ConnectionResult.SUCCESS) { 
//Success! Do what you want 

Log.i("service", "Google play available"); 
} 
else{ 
Log.i("service", "No Google play available"); 
} 

PolygonOptions rectOptions = new PolygonOptions() 
.add(new LatLng(35.25010,-90.08342), 
new LatLng(35.25010,-90.04175), 
new LatLng(35.29177,-90.04175), 
new LatLng(35.29177,-90.08342), 
new LatLng(35.25010,-90.08342)); 


//Set the rectangle's stroke color to red 
rectOptions.strokeColor(Color.BLUE); 
//Set the rectangle's fill to blue 
rectOptions.fillColor(Color.CYAN); 
rectOptions.strokeWidth(2); 
//Get back the mutable Polygon 
polygon = googleMap.addPolygon(rectOptions); 

} 

} 

在谷歌地圖API V1的onTap方法用於與多邊形進行交互。我的問題是在版本2中獲取onTap事件的最佳方式是什麼?我已閱讀Google文檔,除非我錯過了它,否則我沒有看到任何內容。

回答

1

現在,您將不得不使用OnMapClickListener並自己實現「點內多邊形」算法(使用谷歌可以輕鬆實現)。

0

您需要檢查多邊形中每2個連續點的方向,以及響應用戶點擊的方向。當你調用該函數用於形成多邊形的所有點

public boolean orientation(float lat1, float lon1, float lat2, float lon2, float lat3, float lon3){ 
    float result = lat1*lon2 + lat2*lon3 + lat3*lon1 - lon2*lat3 - lon3*lat1 - lon1*lat2; 
    if(result >= 0) return true; 
    else return false; 
} 

:這是檢查從3點與第三所做的矩陣的行列式的符號座標0拿這個函數中進行的那樣一邊和所有他們返回相同的結果 - 點在多邊形內。

+0

回答這個問題,讓我在正確的道路上:http://stackoverflow.com/questions/26017892/android-check-if-my-lat-lng-position-withing-a-given-面積 –

2

請嘗試用此爲Polygon並閱讀此鏈接。

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polygon 

GoogleMap map; 

    Polygon polygon = map.addPolygon(new PolygonOptions() 
.add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0)) 
.strokeColor(Color.RED) 
.fillColor(Color.BLUE)); 
+0

你可以插入4個拉格朗來繪製,你也可以給予布里格的顏色...... – Amitsharma