代码之家  ›  专栏  ›  技术社区  ›  Sophie

在位置更改时将标记设置为当前位置的动画

  •  1
  • Sophie  · 技术社区  · 7 年前

    以下是我使用的:

    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

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

    @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));
    }
    
    4 回复  |  直到 7 年前
        1
  •  3
  •   SO Profile    7 年前

    这是 final 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.

        2
  •  0
  •   arjun babu    7 年前

    做一件事,放一个每2秒重复一次的背景线程。删除上一个标记并将标记设置在此线程内。我认为这是一种可能的方式。如果移动,经度和纬度也会发生变化,因此标记每2秒移动一次。

        3
  •  0
  •   Dorukhan Arslan    7 年前

    当您的位置发生变化时,无需删除标记并将其再次添加到地图上。您可以按如下方式设置标记的位置。

    https://gist.github.com/broady/6314689

    如果位置更新 :

    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
  •  0
  •   UsamaAmjad    6 年前

    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));
            }
    }