繁体   English   中英

如何将camra移动到Android中的谷歌地图v2中的特定标记

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM