2013-05-04 26 views
3

我想要一個類似於雷達屏幕的東西,該屏幕集中在用戶所在的位置,同時縮放足以包含v2 api的目標點。現在,我使用將Android GoogleMap放在一個點上,同時縮放得足以包含另一個點

bounds = new LatLngBounds.Builder().include(destPos) 
       .include(yourPos).build(); 
     map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50)); 

但這中心在兩個點之間的某一點,並放大到包括。有沒有一種方法可以像我想要的那樣簡單?或者我必須或多或少地從頭開始並做一些數學運算(例如,計算兩點之間的距離,計算LatLngBounds的lat/lng,以便用戶位於定義的矩形的中心並且矩形的邊緣包括目的地 - 考慮地圖/屏幕尺寸)?

這是我有:

enter image description here

這是我想要的東西:

enter image description here

+0

@ MES-請儘可能添加屏幕截圖。 – TheFlash 2013-05-04 04:10:08

+0

添加屏幕截圖。 – 2013-05-04 04:43:44

+0

您可以將地圖居中放在第一個點,然後縮小,直到第二個點可見。我沒有嘗試過這個,但也許你可以使用'GoogleMap#getProjection()。getVisibleRegion()。latLngBounds'來獲取當前可見的地圖區域,然後使用'LatLngBounds#contains ()'。如果第二個點不可見,則可以縮小並再次檢查,直到該點可見。我會嘗試這個爲我的項目之一,如果它的作品,我會發佈一個答案。 – rubenlop 2013-05-04 05:08:02

回答

4

你自己的答案是過於複雜。

您需要計算位置另一側的點,並將其包含在LatLngBounds中。

LatLng otherSidePos = new LatLng(2 * yourPos.latitude - destPos.latitude, 2 * yourPos.longitude - destPos.longitude); 
bounds = new LatLngBounds.Builder().include(destPos) 
       .include(otherSidePos).build(); 
     map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding)); 

注意:您不想將50像素作爲填充進行硬編碼。它在不同的設備上看起來不同。改用密度獨立的像素。

+0

絕對更優雅;我會用這個來代替。順便說一句,我沒有填充50像素,但1.5英寸的距離,以latlngbounds。 – 2013-05-05 17:32:00

+0

我會建議(因爲我的解決方案崩潰,因爲地圖還沒有佈局)使用另一種方法來設置動畫:'map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,metrics)。(); getForfaultDisplay()。getMetrics(metrics);' – Vive 2013-09-01 17:02:56

+0

@Vive如果你碰巧遇到了這個問題,那麼可以使用DisplayMetrics指標= new DisplayMetrics(); getWindowManager()。在佈局完成之前調用2個參數版本將會崩潰[如文檔中所述](https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory#newLatLngBounds %28com.google.android.gms.maps.model.LatLngBounds,%20int%29)。您可以自由使用4個params版本或在版面設計完成後調用它。 – 2013-09-01 17:59:06

0

我想通了,如何做到這一點的硬路上面給出。我選擇了LatLngBounds的一個方形區域來簡化軸承編號(只要它適合在屏幕中,這對我來說是可以的:你可以計算出軸承從中心到非方形矩形的角點與一些更多的三角)。我還添加了一些填充,因爲點之間的距離將與從中心到東北角的對角線相同,並且這將大於從中心到頂邊的距離,這將意味着第二個點可能在頂部邊緣之上並且不可見(如果是,例如正北)。我借巨資從http://www.movable-type.co.uk/scripts/latlong.htmlcalculating Lat and Long from Bearing and Distance

這是我對獲得的LatLngBounds方法:

public LatLngBounds getBounds(double lat1, double lng1, double lat2, 
     double lng2) { 

    // defines a square area with point lat1,lng1 at center and includes 
    // point 
    // lat2, long2 with a buffer between edge and second point 

    // get distance between two points 
    float[] results = new float[1]; 

    Location.distanceBetween(lat1, lng1, lat2, lng2, results); 
    double d = results[0]; 

    d = (d * 1.5); // add padding. The shortest distance to an edge of the 
//square box is d * cos(45 degrees). Thus it's possible that the second point will be out of the box. To compensate I make the radius larger (d * 1/cos(45) = d * 1.41). I put a bit extra so that the second point isn't right on the edge of the screen. 

    long R = 6371000; // distance of earth's radius in meters 

    d = d /(double) R; 

    lat1 = Math.toRadians(lat1); // Current lat point converted to radians 
    lng1 = Math.toRadians(lng1); // Current long point converted to radians 

    // calculate northeast corner of LatLngBounds 

    double brng = Math.toRadians(45); // bearing from center to northeast in 
             // radians 

    double resultLat1 = Math.asin(Math.sin(lat1) * Math.cos(d) 
      + Math.cos(lat1) * Math.sin(d) * Math.cos(brng)); 

    double resultLng1 = lng1 
      + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat1), 
        Math.cos(d) - Math.sin(lat1) * Math.sin(resultLat1)); 

    resultLat1 = Math.toDegrees(resultLat1); 
    resultLng1 = Math.toDegrees(resultLng1); 

    Log.i("My Code", "resultLat1: " + resultLat1 + " resultLng1: " + resultLng1); 

    LatLng northEast = new LatLng(resultLat1, resultLng1); 

    // calculate southwest corner of LatLngBounds. Everything is the same 
    // except the bearing value 

    brng = Math.toRadians(225); // bearing from center to southwest corner 
           // in radians 
    double resultLat2 = Math.asin(Math.sin(lat1) * Math.cos(d) 
      + Math.cos(lat1) * Math.sin(d) * Math.cos(brng)); 

    double resultLng2 = lng1 
      + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat1), 
        Math.cos(d) - Math.sin(lat1) * Math.sin(resultLat2)); 

    resultLat2 = Math.toDegrees(resultLat2); 
    resultLng2 = Math.toDegrees(resultLng2); 

    Log.i("My Code", "resultLat2: " + resultLat2 + " resultLng2: " + resultLng2); 

    LatLng southWest = new LatLng(resultLat2, resultLng2); 

    LatLngBounds bounds = new LatLngBounds(southWest, northEast); 

    return bounds; 
} 
0

用於縮放以包含所有標記而不更改相機目標(地圖中心)。

LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
for (Marker marker : markers) { 
    builder.include(marker.getPosition()); 
} 
LatLngBounds bounds = builder.build(); 

LatLng currentLoc = _googleMapView.getCameraPosition().target; 

//make sure that centre location doesn't change 
double deltaLat = Math.max(Math.abs(bounds.southwest.latitude - currentLoc.latitude), 
       Math.abs(bounds.northeast.latitude - currentLoc.latitude)); 

double deltaLon = Math.max(Math.abs(bounds.southwest.longitude - currentLoc.longitude), 
       Math.abs(bounds.northeast.longitude - currentLoc.longitude)); 

LatLngBounds.Builder displayBuilder = new LatLngBounds.Builder(); 
displayBuilder.include(currentLoc); //not necessary but hey 
displayBuilder.include(new LatLng(currentLoc.latitude + deltaLat, currentLoc.longitude + deltaLon)); 
displayBuilder.include(new LatLng(currentLoc.latitude - deltaLat, currentLoc.longitude - deltaLon)); 

_googleMapView.moveCamera(CameraUpdateFactory.newLatLngBounds(displayBuilder.build(), 0)); 
相關問題