[英]how to move camra to a specific marker in google maps v2 in android
第一次用戶查看地圖時,我能夠顯示標記以及縮放和相機設置顯示標記。 但是我的要求是,如果用戶在訪問期間離開該標記位置(標記離開屏幕),則將攝像機移動到相同的標記位置(用戶需要時)。
引用了GoogleMap對象和Marker后,您可以簡單地使用
GoogleMap mMap;
Marker mMarker;
[...]
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mMarker.getPosition(), 14));
(您可以用“ 14”代替所需的縮放級別)。
只需將該行附加到用戶將單擊以“返回”標記的按鈕的OnClick事件上,即可完成操作! ;)
感謝您的答復,但我一直在尋找一些本機Map組件來執行地圖標記重置任務,而不是外部按鈕來導航回所需的標記位置。 我使用以下代碼在Map Api中使用了最新更新(具有setOnMyLocationButtonClickListener):
mMap.setMyLocationEnabled(true);
LatLng markerLoc=new LatLng(companyDetail.getLatitude(), companyDetail.getLongitude());
final CameraPosition cameraPosition = new CameraPosition.Builder()
.target(markerLoc) // Sets the center of the map to Mountain View
.zoom(13) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); //
mMap.addMarker(new MarkerOptions().position(new LatLng(companyDetail.getLatitude(), companyDetail.getLongitude())).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return true;
}
});
您可以使用GoogleMap對象的[animateCamera] [1]函數
GoogleMap googleMap = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map).getMap();
googleMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
[1]: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#animateCamera%28com.google.android.gms.maps.CameraUpdate%29
您也可以這樣使用:
LatLng cur_Latlng=new LatLng(21.0000,78.0000); // giving your marker to zoom to your location area.
gm.moveCamera(CameraUpdateFactory.newLatLng(cur_Latlng));
gm.animateCamera(CameraUpdateFactory.zoomTo(4));
//另一種方法是使用當前位置
@Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 4);
gm.animateCamera(cameraUpdate);
Marker myMarkerthirtyfour = gm.addMarker(new MarkerOptions()
.position(latLng)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
locationManager.removeUpdates(this);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.