2016-09-28 37 views
1

我構建了一個應用程序,它使用帶有多個標記的GMaps。用戶可以添加或刪除標記,但我的問題是選擇好的縮放級別來顯示所有的標記。如何在Google地圖上選擇正確的縮放比例

如果用戶添加或刪除標記,則縮放級別可能會更改。

定心我使用下面的代碼相機:

double xMin = 90.0 ; 
double xMax = 0.0 ; 
double yMin = 90.0 ; 
double yMax = 0.0 ; 

LatLngBounds.Builder builder = new LatLngBounds.Builder(); 

for (Place place : places) { 
       if (place.getLatitude() < xMin) 
        xMin = place.getLatitude(); 

       if (place.getLatitude() > xMax) 
        xMax = place.getLatitude(); 

       if (place.getLongitude() < yMin) 
        yMin = place.getLongitude(); 

       if (place.getLongitude() > yMax) 
        yMax = place.getLongitude(); 

       MarkerOptions markerOptions = new MarkerOptions(); 
       markerOptions.position(new LatLng(place.getLatitude(), place.getLongitude())); 
       markerOptions.title(String.valueOf(place.getId())); 
       Marker marker = this.googleMap.addMarker(markerOptions); 
       allMarkersMap.put(marker, place); 
       displayMarkersMap.put(marker, place); 
       builder.include(marker.getPosition()); 
      } 

      double xPos = (xMax + xMin)/2; 
      double yPos = (yMax + yMin)/2; 

      LatLngBounds bounds = builder.build(); 
      int padding = 1; // offset from edges of the map in pixels 
      //CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding); 
      //this.googleMap.moveCamera(cameraUpdate); 
      LatLng latLng = new LatLng(xPos, yPos); 
      CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(2).build(); 
      this.googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

但我沒有找到一個解決方案,具有良好的縮放值。

+0

我認爲最好的選擇是使用'CameraUpdateFactory.newLatLngBounds(bounds,padding);'你已經在你的代碼 – antonio

+0

中評論好了,但是如何動態地選擇ag ood縮放級別?我想選擇顯示地圖上所有標記的縮放級別 – Fred37b

回答

2

我明白,你需要定義一箇中心,爲您的地圖,並確保所有的標記是在視圖中。

在這種情況下,你可以計算,其中包括您的標記,然後LatLngBounds使用從Google Maps API Utility Library

private LatLngBounds findBounds(List<LatLng> positions, LatLng center) { 
    // Add all the positions to a LatLngBounds (including center) 
    LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder(); 
    for (LatLng position : positions) { 
     boundsBuilder.include(position); 
    } 
    boundsBuilder.include(center); 
    LatLngBounds bounds = boundsBuilder.build(); 

    // Compute the farthest location from the center among the bound corners 
    LatLng northWest = new LatLng(bounds.northeast.latitude, bounds.southwest.longitude); 
    LatLng southEast = new LatLng(bounds.southwest.latitude, bounds.northeast.longitude); 
    LatLng farthest = findFarthestLocation(center, bounds.northeast, northWest, southEast, bounds.southwest); 

    // Expand the bounds adding the projection of the farthest location from the center 
    // in the oposite direction 
    bounds = bounds.including(SphericalUtil.computeOffset(center, 
      SphericalUtil.computeDistanceBetween(center, farthest), 
      SphericalUtil.computeHeading(center, farthest) + 180)); 

    return bounds; 
} 

private LatLng findFarthestLocation(LatLng from, LatLng... locations) { 
    LatLng farthest = null; 

    for (LatLng location : locations) { 
     if (farthest == null || 
       SphericalUtil.computeDistanceBetween(from, location) > SphericalUtil.computeDistanceBetween(from, farthest)) { 
      farthest = location; 
     } 
    } 

    return farthest; 
} 

SphericalUtil.computeOffsetSphericalUtil.computeDistanceBetweenSphericalUtil.computeHeading方法來擴展它可以測試這種方法做

LatLng center = new LatLng(40.384213, -3.875244); 
List<LatLng> positions = new ArrayList<>(); 
positions.add(new LatLng(43.153102, 2.914307)); 
positions.add(new LatLng(42.976521, 1.508057)); 
positions.add(new LatLng(42.492353, 0.123779)); 

Display display = getActivity().getWindowManager().getDefaultDisplay(); 
       Point size = new Point(); 
       display.getSize(size); 
       int width = size.x; 
       int height = size.y; 

LatLngBounds bounds = findBounds(positions,center); 
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,width,height,10)); 
+0

好的,謝謝,但你如何選擇一個好的縮放級別? – Fred37b

+0

你不需要選擇一個縮放級別,使用這段代碼將視圖自動調整爲顯示所有標記,確保地圖居中在所需點 – antonio

+0

好吧,它似乎工作,但我有一個例外,IllegalStateException這是因爲我沒有計算出我的屏幕尺寸。 – Fred37b

0
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_MYPOSITION, 16); 
map.animateCamera(update); 

試試這個,我覺得16變焦將足以看到標記

+0

我想根據標記位置動態選擇縮放級別,我無法爲縮放級別選擇自己的值。 – Fred37b

相關問題