【问题标题】:Android - Changing position of a marker dynamically with google maps apiAndroid - 使用谷歌地图 api 动态更改标记的位置
【发布时间】:2015-02-04 17:30:36
【问题描述】:

我正在编写一个应用程序来跟踪将其当前坐标(经度和纬度)发送到服务器的设备。我正在尝试使用 google maps api 不断更新我的地图上的标记。但是,我在这方面非常不成功。

我正在解析来自服务器的数据,因此我拥有更新标记所需的坐标。但是,如果我执行 marker.remove() 或 marker.setPosition(latilongi),应用程序只会绘制从服务器解析的最后一个坐标。

如果有人对如何实现这种能够运行 android 应用程序并看到标记动态变化的影响有任何见解,将不胜感激!

提前致谢

【问题讨论】:

  • 当你使用marker.setPosition(latilongi)时会发生什么??
  • 它只会将标记设置为从服务器解析的最后一个位置,并且不会在最终位置之前的位置显示标记
  • 所以你希望以前的标记与新添加的标记一起出现在那儿......对吗???
  • 不是真的,我希望标记像当前位置标记一样,因为当您的位置发生变化时它会不断移动。
  • ooo 完全糊涂了...这不是你现在真正得到的吗???

标签: android google-maps google-maps-markers marker


【解决方案1】:

您可以使用它来移动带有动画的标记,使用它:

  /**
   * @param marker the operation marker
   * @param finalPosition the destination latlng
   * @param latLngInterpolator interpolator of animation
   */
static void animateMarkerToICS(Marker marker, LatLng finalPosition, final LatLngInterpolator     latLngInterpolator)

这是代码: https://gist.github.com/broady/6314689

和视频: https://www.youtube.com/watch?v=WKfZsCKSXVQ

【讨论】:

    【解决方案2】:

    听起来您想使用animateCamera()updateCamera()

    您可以在下面找到一些详细信息,但基本上如果您知道地图的新中心,您将执行以下操作:

    public void centerMapAt(LatLng latLng) {
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    }
    

    public void centerMapAt(LatLng latlng, LatLngBounds bounds) {
        int padding = 100; 
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
            getMap().animateCamera(cameraUpdate);       
    }
    

    有很多选择。

    http://developer.android.com/reference/com/google/android/gms/maps/CameraUpdateFactory.html

    http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#animateCamera(com.google.android.gms.maps.CameraUpdate)

    【讨论】:

    • 好的,谢谢。我会试试看!我从我的 onCreate() 方法中调用此方法是否正确?
    • 我认为您每次从服务器获取新位置时都希望调用它。我所做的是在地图处于活动状态时每 10 秒轮询一次服务器,因此每 10 秒我会获得一个新位置,然后更新我的标记并通过 cameraAnimate(newLocation) 重新定位地图;
    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多