1

I have two markers, namely startLocation and the other is stopLocation. startLocation will detect the user's current location, and then the user will walk, and when they stop they'll press stop and stopLocation will be captured as their new current location. I want to draw a polyline as the user is moving from the startLocation to stopLocation. Alternatively, the polyline can also be drawn after both markers for start and stop location has been created - whichever is more implementable. How can this be done? Most of the answers refer to retrieving routes and then drawing the polylines, but that's not what I want - I want to get the user's personalized route. In short, I want to record the route the user has taken. I've managed to create both markers already:

btnStart = (Button) findViewById(R.id.btnStart);
    btnStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // Create Start Marker
            // get current location
            LocationManager locManager;
            String context = Context.LOCATION_SERVICE;
            locManager = (LocationManager) getSystemService(context);

            Criteria c = new Criteria();
            c.setAccuracy(Criteria.ACCURACY_FINE);
            c.setAltitudeRequired(false);
            c.setBearingRequired(false);
            c.setCostAllowed(true);
            c.setPowerRequirement(Criteria.POWER_LOW);

            String provider = locManager.getBestProvider(c, true);
            Location loc = locManager.getLastKnownLocation(provider);

            LatLng currentPosition = updateWithNewLocation(loc);

            Marker startLocation = map.addMarker(new MarkerOptions()
                    .position(currentPosition)
                    .title("Start Location")
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

            map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 17));


        }

    });

    btnStop = (Button) findViewById(R.id.btnStop);
    btnStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // Create Stop

            // get current location
            LocationManager locManager;
            String context = Context.LOCATION_SERVICE;
            locManager = (LocationManager) getSystemService(context);

            Criteria c = new Criteria();
            c.setAccuracy(Criteria.ACCURACY_FINE);
            c.setAltitudeRequired(false);
            c.setBearingRequired(false);
            c.setCostAllowed(true);
            c.setPowerRequirement(Criteria.POWER_LOW);

            String provider = locManager.getBestProvider(c, true);
            Location loc = locManager.getLastKnownLocation(provider);

            LatLng currentPosition = updateWithNewLocation(loc);

            Marker stopLocation = map.addMarker(new MarkerOptions()
                    .position(currentPosition)
                    .title("Stop Location")
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));

            map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 17));

            // Draw dynamic line
        }

    });

Now all I need is to draw the line between the two markers. Thanks!

user3763216
  • 489
  • 2
  • 10
  • 29

1 Answers1

1

There is no way to do this without tracking the user's location. You should use the requestLocationUpdates function to listen and get the update of your user's location. Refer to the developer guide for more information on listening to the GPS location.

String locationProvider = LocationManager.NETWORK_PROVIDER;
// Or, use GPS location data:
// String locationProvider = LocationManager.GPS_PROVIDER;

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

You might also want to use the snap to road function in the newly released Google Maps Road API, to fix your raw lat/lng from GPS, and get a smoother path on the road. It does not currently have Android APIs, so you might need to use the Web API to access the snap to road service.

https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836
        &interpolate=true
        &key=API_KEY

After users stopped tracking or reached the end point, you can create a polyline based on their path.

// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

Let me know if it is not clear, and hope it helps.

kaho
  • 4,746
  • 1
  • 16
  • 24