避免在android中的谷歌地图动画上使用相同的Lat-Lng



我使用这个例子在谷歌地图中实现动画。我正在从服务器获取LatLng数据。但当我为地图上的一个点获取了太多数据时,问题就出现了。只要有单个点的数据,动画标记就会在该点上等待。请参阅以下示例代码

for (i = 0; i < jsonAssets.length(); i++) {
                        JSONObject nlmJsonObject = jsonAssets.getJSONObject(i);
                        JSONObject lastData_JsonObject = nlmJsonObject.getJSONObject("last_data");
                        JSONObject nlm_locJsonObject = lastData_JsonObject.getJSONObject("loc");
                        JSONArray coordinated = nlm_locJsonObject.getJSONArray("coordinates");
                        Log.d("LASTLATLONG_ANIMATION", coordinated.toString());
                        latt = (double) coordinated.get(0);
                        lng = (double) coordinated.get(1);
                        addMarkerToMap(new LatLng(lng, latt));
                    }    
                    animator.startAnimation(true);

如何检查和省略类似的点以使贴图动画更平滑?

感谢

刚刚编辑了您的代码,给出了的想法

private double latt;
private double lng;
private void showMarkers(){
    // Your other codes
    for (i = 0; i < jsonAssets.length(); i++) {
        JSONObject nlmJsonObject = jsonAssets.getJSONObject(i);
        JSONObject lastData_JsonObject = nlmJsonObject.getJSONObject("last_data");
        JSONObject nlm_locJsonObject = lastData_JsonObject.getJSONObject("loc");
        JSONArray coordinated = nlm_locJsonObject.getJSONArray("coordinates");
        Log.d("LASTLATLONG_ANIMATION", coordinated.toString());
        // Current values
        double currentLat = (double) coordinated.get(0);
        double currentLng = (double) coordinated.get(1);
        // Compare. If not same add the marker
        if(currentLat != latt && currentLng != lng){
            latt = (double) coordinated.get(0);
            lng = (double) coordinated.get(1);
            addMarkerToMap(new LatLng(lng, latt));
        }
    }
    animator.startAnimation(true);
}

最好的选择是,只有当新标记的位置距离最后添加的标记超过给定距离时,才添加新标记。

我的示例使用Google Maps API实用程序库中的SphericalUtil.computeDistanceBetween来计算两个LatLng对象之间的距离:

final float MIN_DISTANCE = 10; // Minimum distance in meters to consider that two locations are the same
LatLng lastLatLng = null;
// ...
for (i = 0; i < jsonAssets.length(); i++) {
    JSONObject nlmJsonObject = jsonAssets.getJSONObject(i);
    JSONObject lastData_JsonObject = nlmJsonObject.getJSONObject("last_data");
    JSONObject nlm_locJsonObject = lastData_JsonObject.getJSONObject("loc");
    JSONArray coordinated = nlm_locJsonObject.getJSONArray("coordinates");
    Log.d("LASTLATLONG_ANIMATION", coordinated.toString());
    latt = (double) coordinated.get(0);
    lng = (double) coordinated.get(1);
    LatLng currentLatLng = new LatLng(latt, lng);
    if (lastLatLng == null || SphericalUtil.computeDistanceBetween(lastLatLng, currentLatLng) > MIN_DISTANCE) {
        addMarkerToMap(new LatLng(lng, latt));
        lastLatLng = currentLatLng;
    }
}
animator.startAnimation(true);

最新更新