【问题标题】:How to animate the camera to a specified location in Google Maps v2 for Android?如何在适用于 Android 的 Google Maps v2 中将相机动画到指定位置?
【发布时间】:2015-08-22 23:30:15
【问题描述】:

我无法将 Google 地图相机移回初始位置。基本上,我在操作栏上有一个主页按钮。如果用户在地图上滚动并点击主页按钮,我希望相机移回其原始位置。

我使用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:

<?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>

我错过了什么?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    每当用户选择该选项时,您就在使用

    LatLng initialLoc= mMap.getCameraPosition().target;
    

    获取所谓的初始位置,这是错误的! mMap.getCameraPosition().target 返回相机指向的位置。您应该根据其他代码将 lat long 存储在活动的onCreate() 或其他位置,然后在onOptionItemSelected() 中使用相同的位置。

    顺便说一句,您可以在一个语句中组合缩放和纬度,如下所示。

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

    更新

    我真的不知道它有多准确或何时调用它。但是你可以使用相同的代码

    LatLng initialLoc= mMap.getCameraPosition().target;
    

    而是在您的onCreate()onResume() 中调用一次,然后将其存储在那里。然后下次在您的optionsItemSelected() 中使用这些值。虽然,你为什么不简单地将你在 xml 中定义的那些值存储在 java 代码中然后使用它呢?

    【讨论】:

    • 你是对的!我怎么能得到我的相机的初始坐标。我的相机的初始坐标是使用cameraTargetLatcameraTargetLat 在我的.xml 中定义的。如何以编程方式检索它?
    • 你说的是哪个xml?
    • 我更新了我的帖子,我喜欢在不为坐标定义新常量的情况下检索相机的目标纬度和经度。是否可以通过编程方式从 .xml 中检索这些值?
    【解决方案2】:
         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)); 
    

    【讨论】:

      【解决方案3】:
      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));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-13
        • 2020-12-25
        • 1970-01-01
        • 2018-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多