¿Cómo agregar marcadores en las polilíneas de Google Maps en función de la distancia rgw a lo largo de una línea en Android?

1 minuto de lectura

avatar de usuario de creativecoder
codificador creativo

He dibujado una línea entre dos puntos en google maps. Ahora quiero agregar marcadores en esa línea para una distancia particular.

[![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?

  • Take a look at stackoverflow.com/questions/38497130/…

    – antonio

    Mar 31, 2019 at 21:08


  • 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.

    – user2711811

    Apr 1, 2019 at 0:38


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)

  • stackoverflow.com/questions/38497130/… this link has given me the answer, thanks bro

    – creativecoder

    Apr 3, 2019 at 7:49


¿Ha sido útil esta solución?