1

How can I move marker to current Location

I am already experienced with two fixed geo locations

Here is what I used:

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

Then calling it within onMapReady(...)

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

    autoAnimateMarker();

}

TO SHOW CURRENT LOCATION

I have already gone through - https://stackoverflow.com/a/34582595/3585072

Currently onLocationChanged(Location location) method looks like this, what I need to put here, to dynamically move my marker based on Location change:

@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));
}
SO Profile
  • 100
  • 5
Sophie
  • 2,594
  • 10
  • 41
  • 75
  • what do you mean with "only when I move" do you mean when the cursor moves, or when you click on the other geo-location? – King Reload Sep 06 '17 at 09:24
  • @KingReload "only when I move" it means based on my current position it should move, like I am driving a car and starting from Location A and target is to reach Location B... so I want this Airplane marker should move with me... Is it clear now ? – Sophie Sep 06 '17 at 11:01
  • Listen your current location periodically, and move the marker whenever your location is changed. I couldn't understand what you exactly want to do. – Dorukhan Arslan Sep 06 '17 at 11:39
  • @DorukhanArslan actually you got my point, I just want to know how can I dynamically move marker, whenever there is a change in location ? still moving statically from Location A to Location B. – Sophie Sep 06 '17 at 12:21
  • Please check code I posted above of onLocationChanged(Location location) method – Sophie Sep 06 '17 at 12:43
  • See my answer below. You have to be sure that your location services are turned on and your device is on move observably. – Dorukhan Arslan Sep 06 '17 at 13:44

4 Answers4

3

This is the final solution I provided to Get, Update and Animate Marker to 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.

SO Profile
  • 100
  • 5
  • I just updated Title of your question... please accept those changes. So I hope you will get more views and answers here as current title confuses I guess. – SO Profile Sep 06 '17 at 18:41
  • But I would like to wait for some other answers – Sophie Sep 06 '17 at 19:07
  • 1
    I am not sure what animateMarkerToICS does. Why you add a new marker at each location update here? What if you change position of the already-initialized-marker-in-onMapReady in onLocationChanged? Is it necessary for some reason? I think it will create multiple markers on your trajectory. – Dorukhan Arslan Sep 06 '17 at 21:25
  • @DorukhanArslan can you show me the best way of doing, can you share how my both the methods should look like actually... onLocationCahanged and onMapReady... I want to see map to be moved from one location to another based on current location... – Sophie Sep 07 '17 at 07:51
  • See my answer above. – Dorukhan Arslan Sep 07 '17 at 08:14
0

do one thing put a background thread which repeats every 2 seconds. Remove previous marker and Set the marker inside this thread. It is one possible way i think. if you moves the longitude and latitude also changes so the marker moves each 2 seconds.

arjun babu
  • 67
  • 1
  • 11
0

There is no need for removing the marker when your location changes and adding it again on map. You can set position of the marker as follows.

Firstly, see: https://gist.github.com/broady/6314689

If location updates interval is more than ~3 seconds:

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

If location updates interval is too short (Do not animate, just move the marker):

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

You should initialize the marker in onMapReady instead of onLocationChanged.

Dorukhan Arslan
  • 2,676
  • 2
  • 24
  • 42
0

MarkerAnimation class was not available in my API version. Below is how I did it in my app.

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));
        }
}
UsamaAmjad
  • 4,175
  • 3
  • 28
  • 35