Android GoogleMaps V2不刷新

4
我的应用程序显示用户的行驶路线。几秒钟后,地图停止加载新内容,看起来像这样:enter image description here 我的代码: 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);

有没有一种方法可以使 Map 失效?
感谢您的帮助!

有人有想法吗?我还在苦恼这个问题... - iTamp
3个回答

0

好的,对于那些遇到同样问题的人,这里是解决方案。问题在于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));
}

1
更改相机位置可能会导致向谷歌发出新的地图请求,并最终从您的配额中扣除。请注意!尽可能避免使用相机重新定位。 - Johann

0
在更新地图标记、覆盖物等后,请使用Thread.sleep(300);。Google Maps使用自己的内部线程,需要时间来应用更改。有可能添加内容到地图的线程正在阻塞Google Map的线程。休眠将为其提供一些时间来进行更新。

0

在遇到问题之前,我并不知道这个方法。但是我解决了,并且想与您分享。 您可以使用handler和Thread.sleep(600)。如果只使用handler,您将遇到相同的问题。Thread.sleep函数等待地图更新,然后继续标记。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接