【问题标题】:Animate marker to current location on location changed在位置更改时将标记动画到当前位置
【发布时间】:2018-02-11 23:27:12
【问题描述】:

如何将标记移动到当前位置

我已经熟悉了两个固定的地理位置

这是我使用的:

private void autoAnimateMarker() {

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    mMap.getUiSettings().setZoomControlsEnabled(true);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    mMap.setMyLocationEnabled(true);

    LatLng fromLocation = new LatLng(-33.904438, 151.249852); 
    LatLng toLocation = new LatLng(-33.905823, 151.252422);
    Marker marker = mMap.addMarker(new MarkerOptions().position(fromLocation));
    MarkerAnimation.animateMarkerToICS(marker, toLocation, new LatLngInterpolator.Spherical());

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(fromLocation, 17F));
}

然后在onMapReady(...)内调用它

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    autoAnimateMarker();

}

显示当前位置

我已经经历过了-https://stackoverflow.com/a/34582595/3585072

目前 onLocationChanged(Location location) 方法看起来像这样,我需要放在这里,根据位置变化动态移动我的标记:

@Override
public void onLocationChanged(Location location)
{
    Toast.makeText(this, "Location Changed " + location.getLatitude()
            + location.getLongitude(), Toast.LENGTH_LONG).show();

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);

    //move map camera
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
}

【问题讨论】:

  • “仅当我移动时”是什么意思,是指光标移动时,还是单击其他地理位置时?
  • @KingReload“仅当我移动时”这意味着根据我当前的位置它应该移动,就像我正在驾驶汽车并从位置 A 开始,目标是到达位置 B ......所以我希望这个飞机标记应该和我一起移动......现在清楚了吗?
  • 定期收听您当前的位置,并在您的位置更改时移动标记。我不明白你到底想做什么。
  • @DorukhanArslan 实际上你明白我的意思,我只是想知道如何动态移动标记,只要位置发生变化?仍然从位置 A 静态移动到位置 B。
  • 请检查我在上面发布的 onLocationChanged(Location location) 方法的代码

标签: android google-maps google-maps-markers google-maps-android-api-2


【解决方案1】:

这是我提供给current locationfinal solution获取、更新和动画标记current location

LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

if(ourGlobalMarker == null) { // First time adding marker to map
    ourGlobalMarker = mGoogleMap.addMarker(new MarkerOptions().position(latLng)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));
    MarkerAnimation.animateMarkerToICS(ourGlobalMarker, latLng, new LatLngInterpolator.Spherical());
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
} else {
    MarkerAnimation.animateMarkerToICS(ourGlobalMarker, latLng, new LatLngInterpolator.Spherical());
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
}

Feel free to use this code and let me know if you need any update on this.

【讨论】:

  • 我刚刚更新了您问题的标题...请接受这些更改。所以我希望你能在这里得到更多的观点和答案,因为我猜现在的标题很混乱。
  • 但我想等待一些其他的答案
  • 我不确定 animateMarkerToICS 是做什么的。为什么要在每次更新位置时添加一个新标记?如果你改变 onLocationChanged 中已经初始化的标记在 onMapReady 的位置怎么办?出于某种原因有必要吗?我认为它会在你的轨迹上创建多个标记。
  • @DorukhanArslan 你能告诉我最好的方法吗,你能分享一下我的这两种方法实际上应该是什么样子... onLocationCahanged 和 onMapReady... 我想看到要从地图移动根据当前位置从一个位置到另一个位置...
  • 见我上面的回答。
【解决方案2】:

做一件事,放置一个每 2 秒重复一次的后台线程。删除先前的标记并在此线程内设置标记。这是我认为的一种可能方式。如果你移动经度和纬度也会改变,所以标记每 2 秒移动一次。

【讨论】:

    【解决方案3】:

    当您的位置更改并在地图上重新添加时,无需移除标记。您可以按如下方式设置标记的位置。

    首先,请参阅: https://gist.github.com/broady/6314689

    如果位置更新间隔超过 ~3 秒

    public void onLocationChanged(Location location) {
        LatLng toLocation = new LatLng(location.getLatitude(), location.getLongitude());
        if (fromLocation != null) {
            mAnchorMarker.setPosition(fromLocation);
            MarkerAnimation.animateMarkerToICS(mAnchorMarker, toLocation, new LatLngInterpolator.Spherical());
        }
        fromLocation = toLocation;
    }
    

    如果位置更新间隔太短(不要动画,只需移动标记):

    public void onLocationChanged(Location location) {
        LatLng toLocation = new LatLng(location.getLatitude(), location.getLongitude());
        mAnchorMarker.setPosition(toLocation);
    }
    

    您应该在 onMapReady 而不是 onLocationChanged 中初始化标记。

    【讨论】:

      【解决方案4】:

      MarkerAnimation 类在我的API 版本中不可用。以下是我在我的应用中的操作方式。

      Marker mCurrLocationMarker = null;
      LatLng latLng;
      
      @Override
      public void onLocationChanged(Location location){
      
          latLng = new LatLng(location.getLatitude(), location.getLongitude());
          MarkerOptions markerOptions = new MarkerOptions();
          markerOptions.position(latLng);
          markerOptions.title("My Location");
          markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
      
          if(mCurrLocationMarker == null) { // Add marker and move camera on first time
              mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
              mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
          } else { // Update existing marker position and move camera if required
              mCurrLocationMarker.setPosition(latLng);
      //        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
              }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-01-21
        • 1970-01-01
        • 2017-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-16
        • 2016-11-06
        • 1970-01-01
        相关资源
        最近更新 更多