7

当用户第一次查看地图时,我能够显示一个标记以及使用缩放和相机设置显示它。但是我的要求是如果用户在他/她访问期间离开该标记位置(标记离开屏幕),则将相机移动到相同的标记位置(当用户想要时)。

4

4 回答 4

24

拥有对 GoogleMap 对象和标记的引用,您可以简单地使用

GoogleMap mMap;
Marker mMarker;

[...]

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mMarker.getPosition(), 14));

(您可以将“14”替换为所需的缩放级别)。

只需将该行附加到用户将单击以“返回”到标记的按钮的 OnClick 事件......你就完成了!;)

于 2013-08-28T07:00:51.193 回答
12

感谢您的回复,但我正在寻找一些本地地图组件来执行地图标记重置任务,而不是一个外部按钮来导航回所需的标记位置。我使用 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;
        }
    });
于 2013-09-02T10:16:41.813 回答
7

您可以使用 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
于 2013-08-28T07:02:27.393 回答
2

你也可以这样使用:

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);

    }
于 2013-08-29T06:04:37.707 回答