2

I have drawn a line between two points in google maps. Now I want to add markers in that line for a particular distance.`

[![mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
 @Override
 public void onMapClick(LatLng latLng) {

  MarkerOptions marker = new MarkerOptions().position(
   new LatLng(latLng.latitude, latLng.longitude)).title("New Marker");
  mMap.addMarker(marker).setDraggable(true);

  mTempCollection.add(latLng);
  mPolylineOptions.getPoints().clear();
  mPolylineOptions.addAll(mTempCollection);
  LatLngBounds latLngBounds = getPolygonCenterPoint(mTempCollection);
  double distance = SphericalUtil.computeDistanceBetween(latLngBounds.southwest, latLngBounds.northeast);
  Log.d(TAG, "distance: " + distance);
  int plotmarkers = (int)(distance / 10);
  mPolylineOptions.color(Color.GREEN);
  mMap.addMarker(new MarkerOptions()
   .position(latLngBounds.getCenter()));
  mMap.addPolyline(mPolylineOptions);


 }
});

`In this line I want to add markers of particular distance In this line, I want to add markers of a particular distance.

I have found this link which in javascript can it be done for android?
How to add markers on Google Maps polylines based on distance along the line?

creativecoder
  • 1,470
  • 1
  • 14
  • 23
  • 1
    Take a look at https://stackoverflow.com/questions/38497130/how-can-i-extrapolate-farther-along-a-road/38616162#38616162 – antonio Mar 31 '19 at 21:08
  • 1
    Compute heading using the two points (SphericalUtil.computeHeading) then compute offset using heading, distance and origin point (SphericalUtil.computeOffset(from,distance,heading)) and add marker at offset. –  Apr 01 '19 at 00:38

1 Answers1

0

If you have two markers and you want to place a marker from a certain distance away from the start point of that line, you can use SphericalUtil.interpolate to do the job.

double distance_between_points = SphericalUtil.computeDistanceBetween(startPointLoc, endPointLoc);

// Let's say you want the new marker to be DISTANCE away from the start point

LatLng newMarkerLoc = SphericalUtil.interpolate(startPointLoc, endPointLoc, (float) DISTNACE / distnace_between_points);

Not that here, your DISTANCE can be 1km, 2km, or whatever you want (based on the post you mentioned)

Mahmoud H
  • 81
  • 6
  • https://stackoverflow.com/questions/38497130/how-can-i-extrapolate-farther-along-a-road/38616162#38616162 this link has given me the answer, thanks bro – creativecoder Apr 03 '19 at 07:49