【问题标题】:Android GoogleMaps V2 doesnt refreshAndroid 谷歌地图 V2 不刷新
【发布时间】:2012-12-19 18:25:22
【问题描述】:

我的应用程序显示用户的驾驶路线。几秒钟后,地图停止加载新内容,如下所示:

我的代码: XML

    <fragment 
    xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraZoom="19"
    android:layout_below="@+id/tracking_maps_colorgradient">
</fragment>

Java:

            float zoom =  18;//googleMap.getCameraPosition().zoom;
        LatLng latLng = new LatLng(lastLatitude, lastLongitude);
        locations.add(latLng);
        CameraPosition pos = new CameraPosition.Builder()
            .target(latLng)
            .bearing(bearing)
            .zoom(zoom)
            .tilt(googleMap.getCameraPosition().tilt)
            .build();

        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(pos));
        Polyline p = googleMap.addPolyline(new PolylineOptions()
            .add(latLng)
            .width(15)
            .color(Color.RED)
            .geodesic(true));
        p.setPoints(locations);

有没有办法使地图失效?

感谢您的帮助!

【问题讨论】:

  • 没人知道吗?我还在纠结这个问题……

标签: android google-maps refresh loading invalidation


【解决方案1】:

好的,对于有同样问题的人,这是解决方案。问题是 GoogleMap 不会一直刷新,当用户超出边界时,您必须更改相机位置。

LatLngBounds bounds = this.googleMap.getProjection().getVisibleRegion().latLngBounds;

if(!bounds.contains(new LatLng(gps.getLatitude(), gps.getLongitude())))        {
    //Move the camera to the user's location if they are off-screen!
    CameraPosition cameraPosition = new CameraPosition.Builder()
  .target(new LatLng(gps.getLatitude(), gps.getLongitude()))      
  .zoom(zoom)                   // Sets the zoom
  .bearing(bearing)
  .build();                   // Creates a CameraPosition from the builder
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

【讨论】:

  • 更改摄像头位置可能会导致向 Google 提出新的地图请求,并最终从您的配额中扣除。意识到!尽可能避免使用相机重新定位。
【解决方案2】:

使用 Thread.sleep(300);在使用标记、叠加层等更新地图后。谷歌地图使用自己的内部线程,需要时间来应用更改。向地图添加内容的线程可能会阻塞 Google Map 的线程。睡眠将给它一些时间进行更新。

【讨论】:

    【解决方案3】:

    在您遇到问题之前,我不知道是否存在。但我解决了,我想和你分享。 您可以使用处理程序和 Thread.sleep(600)。如果你只使用处理程序,你会遇到同样的问题。 Thread.sleep 函数等待更新地图,然后继续标记。

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多