2014-05-20 3 views
1

Android에서 Google지도로 작업하고 있습니다. 확대/축소 수준을 조절해야합니다.현재 위치에 따른 확대/축소 마커 위치 android

지도 중심에 mapController.setCenter (current_location)를 사용하여 설정 한 마커가 하나 있습니다.

그리고지도마다 다른 색으로 표시되는 마커가 10 개 이상 있습니다.

예상 결과 : 사람의 현재 위치는 항상 중앙에 있어야하며 녹색 아이콘이 화면 (현재 위치 마커 가운데)에 표시되도록 줌 레벨을 조정해야합니다.

내가 뭘하려 :

INT greenlat = (INT) (. AppCoordinates.getInstance() latitudelist.get (0) * 1E6); int greenlong = (int) (AppCoordinates.getInstance(). thousandselist.get (0) * 1E6); int currentlat = (int) (AppCoordinates.getInstance(). getCurrentLatitude() * 1E6); int currentlong = (int) (AppCoordinates.getInstance(). getCurrentLongitude() * 1E6);

 DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     mapController.zoomToSpan((int) (Math.abs(greenlat - currentlat) * 1), (int)(Math.abs(greenlong - currentlong) * 1));// to have some padding on the edges. 
     mapController.animateTo(new GeoPoint((greenlat + currentlat), (greenlong + currentlong)/2)); 

나는 화면의 마커를 부스에 전시 할 수 있지만 화면 중앙에 표시 할 위치를 원합니다.

+0

이 방법 map.animateCamera 시도 (CameraUpdateFactory.newLatLngZoom (새 LatLng를 (currentlat, currentlong을), 8)); 8은 줌 배율입니다.이 줌 배율은 요구 사항에 따라 주어진 포인트에서 또는 바뀔 수 있습니다. –

+0

안녕하세요 Haresh, animateCamera()라는 메서드가 없으므로 android.maps를 사용하고 있습니다. ( –

답변

1

제 생각에는 LatLngBounds가 포함 된 모든 마커를지도에서 정렬 된 사각형으로 볼 수 있기 때문에 LatLngBounds를 조작하는 것이 좋습니다. 따라서 현재 위치를지도의 중심에서 보는 모든 점을 인식하면됩니다.

  1. 모든 마커를 경계에 넣습니다.
  2. 카메라를 현재 위치로 중심점으로 이동하십시오.
  3. 현재지도의 모든 모서리 4 곳을 가져 와서 경계의 목록에 모으십시오.

이 내 샘플입니다 :

LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
Marker myLocation = mMap.addMarker(new MarkerOptions() 
     .position(new LatLng(2.6,101.5)) 
     .title("My-Current-Location")); 


Marker addMarker1 = mMap.addMarker(new MarkerOptions() 
      .position(new LatLng(2.6,101)) 
      .title("Hello world-1")); 

Marker addMarker2 = mMap.addMarker(new MarkerOptions() 
      .position(new LatLng(2.7,101)) 
      .title("Hello world-2")); 

Marker addMarker3 = mMap.addMarker(new MarkerOptions() 
      .position(new LatLng(3.7,105)) 
      .title("Hello world-3")); 

builder.include(myLocation.getPosition()); 
builder.include(addMarker1.getPosition()); 
builder.include(addMarker2.getPosition()); 
builder.include(addMarker3.getPosition()); 

//haha enough to use for loop for all your markers 

int padding = 100; // offset from edges of the map in pixels 

CameraUpdate cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding); 
mMap.moveCamera(cameraUpdate); 
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation.getPosition())); 

LatLng nrLatLng = mMap.getProjection().getVisibleRegion().nearRight; 
LatLng frLatLng = mMap.getProjection().getVisibleRegion().farRight; 
LatLng nlLatLng = mMap.getProjection().getVisibleRegion().nearLeft; 
LatLng flLatLng = mMap.getProjection().getVisibleRegion().farLeft; 

builder.include(nrLatLng); 
builder.include(frLatLng); 
builder.include(nlLatLng); 
builder.include(flLatLng); 

cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding); 
mMap.animateCamera(cameraUpdate); 
+0

감사합니다. zainoz ... 나는이 샘플이 도움이되기를 바랍니다 :) –