用户跟踪移动标记动画

时间:2018-02-16 08:52:43

标签: java android google-maps

我正在听地图上的用户位置和更新标记。但我的代码删除了前一个标记并在新位置放置了一个新标记。
我想要做的是沿着地图将标记从旧位置移动到新位置。

Marker playermarker;
public void onLocationChanged(final Location location) {

    if (playermarker != null) {
        playermarker.remove();
        playermarker = null;
    }
    LatLng latLng;
    latLng = new LatLng(location.getLatitude(), location.getLongitude());

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Thats me!");
    markerOptions.icon(BitmapDescriptorFactory.fromResource(R.raw.petyr));
    playermarker = googleMap.addMarker(markerOptions);

    //Toast.makeText(getActivity(), "lokasyon degisti",Toast.LENGTH_SHORT).show();

    mydb.child("relics").addValueEventListener(new ValueEventListener() {
    HashMap < String, relic > relics = new HashMap();

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
            relics.put(childSnapshot.getKey(), childSnapshot.getValue(relic.class));
        }

        for (Map.Entry < String, relic > entry: relics.entrySet()) {
            Log.d("asd", "Key = " + entry.getKey() + ", Latitude = " + entry.getValue().getLatitude() + entry.getValue().getLongitude());
            int distance = (int) Math.floor(distance(location.getLatitude(), entry.getValue().getLatitude(), location.getLongitude(), entry.getValue().getLongitude(), 0, 0));
            distancetext.setText("You are " + String.valueOf(distance) + " meters away from relic!");

        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {}
    });
}

1 个答案:

答案 0 :(得分:0)

。在位置更改时不要每次都创建一个新的标记实例,只使用一个标记实例,如果一个实例可用,则只更改该标记的位置,而不是每次都添加新标记。请执行以下更改:

if (playermarker == null) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng).
        markerOptions.title("Thats me!").
  markerOptions.icon(BitmapDescriptorFactory.fromResource(R.raw.petyr));
        playermarker = googleMap.addMarker(markerOptions);
    } else {
        playmarker.setPosition(latLng);
        // also set other things whatever you want
    }