简体   繁体   English

如何在Google Maps v2 for Android中将相机设置为指定位置?

[英]How to animate the camera to a specified location in Google Maps v2 for Android?

I'm having trouble moving the Google Maps camera back to the initial position. 我无法将Google地图相机移回初始位置。 Basically, I have a home button on the Actionbar. 基本上,我在Actionbar上有一个主页按钮。 If a user scrolls around the map, and taps the Home button, I would like the camera to move back to its original location. 如果用户在地图上滚动并点击“主页”按钮,我希望相机移回原始位置。

I get the original coordinates using googleMap.getCameraPosition().target , but the camera doesn't move. 我使用googleMap.getCameraPosition().target获取原始坐标,但相机不移动。

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    LatLng initialLoc= mMap.getCameraPosition().target;

    CameraUpdate update = CameraUpdateFactory.newLatLng(initialLoc);
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);

    mMap.moveCamera(update);
    mMap.animateCamera(zoom);

    return true;
}

map.xml: map.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context="org.test"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        map:cameraTargetLat="xxx.xxx"
        map:cameraTargetLng="xxx.xxx"
        map:cameraZoom="15"
        map:mapType="normal" />

</RelativeLayout>

What is it that I am missing? 我错过了什么?

Whenever the user selects the option, you are using 每当用户选择该选项时,您正在使用

LatLng initialLoc= mMap.getCameraPosition().target;

to get the supposedly initial location, which is wrong! 得到所谓的初始位置,这是错误的! mMap.getCameraPosition().target returns the location that the camera is pointing at. mMap.getCameraPosition()。target返回摄像头指向的位置。 You should store the lat long in the onCreate() of the activity or some other place as per your other code, and then use the same in onOptionItemSelected() . 您应该根据您的其他代码将lat long存储在活动的onCreate()或其他位置,然后在onOptionItemSelected()使用相同的内容。

Btw you can combine the zoom and lat long in a single statement as follows. 顺便说一下,你可以在一个语句中将zoom和lat long结合起来,如下所示。

    LatLng coordinate = new LatLng(lat, lng); //Store these lat lng values somewhere. These should be constant.
    CameraUpdate location = CameraUpdateFactory.newLatLngZoom(
            coordinate, 15);
    mMap.animateCamera(location);

UPDATE UPDATE

I dont really know as to how accurate that'd be or when to call it. 我真的不知道它是多么准确或什么时候打电话给它。 But you could use the same code 但是你可以使用相同的代码

LatLng initialLoc= mMap.getCameraPosition().target;

Instead call this once in your onCreate() , or onResume() and then store it there. 而是在onCreate()onResume()调用一次,然后将其存储在那里。 Then next time in your optionsItemSelected() use those values. 然后下次在您的optionsItemSelected()使用这些值。 Although, why don't you simply store those values that you defined in your xml in java code and then use it? 虽然,为什么不简单地将您在xml中定义的值存储在java代码中然后使用它?

     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());  
     MarkerOptions markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title(totalAddress); //Here Total Address is address which you want to show on marker
     mMap.clear();


    markerOptions.icon(
    BitmapDescriptorFactory
   .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)); 

     markerOptions.getPosition(); 
     mCurrLocationMarker = mMap.addMarker(markerOptions); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 
LatLng coordinate = new LatLng(latitude, longitude);
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(coordinate);
                    markerOptions.title(placeName); //Here Total Address is address which you want to show on marker
                    mGoogleMap.clear();
                    markerOptions.icon(
                            BitmapDescriptorFactory
                                    .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

                    markerOptions.getPosition();
                    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
                    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(coordinate));
                    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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