Plotting coordinates on Route in Gmap (Google Maps Android API) - android

I'm currently working on one Android application using Google map.
My requirement is to draw a route between source-destination and plot markers at every 500 meters on that route.
I have drawn a route, but not getting how to plot markers at every 500 meters. Is there any Google API available to get coordinates on route, or I have to implement any other logic?

Objectives
The objective is getting a list of LatLng coordinates along the route returned by the Directions API web service at every N meters. Later we can create markers for this list of coordinates.
Solution
The solution has two steps. The first one is getting a list of LatLng that form a route returned by Directions API. You can use a Java Client for Google Maps Services to execute Directions API request and extract a list of LatLng. Have a look at private List<LatLng> getDirectionsPathFromWebService(String origin, String destination) method in my example. This method calls Directions API and loop through legs and steps of the route object to get a complete list of LatLng that form a route.
The second step is implemented in the method private List<LatLng> getMarkersEveryNMeters(List<LatLng> path, double distance). It loops through all LatLng from the first step and creates a list of LatLng at every N meters where N is a distance in meters passed as a second parameter of the method. This method uses internally SphericalUtil class from the Google Maps Android API Utility Library. Have a look at comment to figure out what is happening in this method.
Finally, I create markers from the list that was obtained in second step.
Code snippet
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private String TAG = "so47784512";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
String origin = "Avinguda Diagonal, 101, 08005 Barcelona, Spain";
String destination = "Carrer de ParĂ­s, 67, 08029 Barcelona, Spain";
LatLng center = new LatLng(41.391942,2.179413);
//Define list to get all latlng for the route
List<LatLng> path = this.getDirectionsPathFromWebService(origin, destination);
//Draw the polyline
if (path.size() > 0) {
PolylineOptions opts = new PolylineOptions().addAll(path).color(Color.BLUE).width(5);
mMap.addPolyline(opts);
}
List<LatLng> markers = this.getMarkersEveryNMeters(path, 500.0);
if (markers.size() > 0) {
for (LatLng m : markers) {
MarkerOptions mopts = new MarkerOptions().position(m);
mMap.addMarker(mopts);
}
}
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 13));
}
private List<LatLng> getDirectionsPathFromWebService(String origin, String destination) {
List<LatLng> path = new ArrayList();
//Execute Directions API request
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyBrPt88vvoPDDn_imh-RzCXl5Ha2F2LYig")
.build();
DirectionsApiRequest req = DirectionsApi.getDirections(context, origin, destination);
try {
DirectionsResult res = req.await();
//Loop through legs and steps to get encoded polylines of each step
if (res.routes != null && res.routes.length > 0) {
DirectionsRoute route = res.routes[0];
if (route.legs !=null) {
for(int i=0; i<route.legs.length; i++) {
DirectionsLeg leg = route.legs[i];
if (leg.steps != null) {
for (int j=0; j<leg.steps.length;j++){
DirectionsStep step = leg.steps[j];
if (step.steps != null && step.steps.length >0) {
for (int k=0; k<step.steps.length;k++){
DirectionsStep step1 = step.steps[k];
EncodedPolyline points1 = step1.polyline;
if (points1 != null) {
//Decode polyline and add points to list of route coordinates
List<com.google.maps.model.LatLng> coords1 = points1.decodePath();
for (com.google.maps.model.LatLng coord1 : coords1) {
path.add(new LatLng(coord1.lat, coord1.lng));
}
}
}
} else {
EncodedPolyline points = step.polyline;
if (points != null) {
//Decode polyline and add points to list of route coordinates
List<com.google.maps.model.LatLng> coords = points.decodePath();
for (com.google.maps.model.LatLng coord : coords) {
path.add(new LatLng(coord.lat, coord.lng));
}
}
}
}
}
}
}
}
} catch(Exception ex) {
Log.e(TAG, ex.getLocalizedMessage());
}
return path;
}
private List<LatLng> getMarkersEveryNMeters(List<LatLng> path, double distance) {
List<LatLng> res = new ArrayList();
LatLng p0 = path.get(0);
res.add(p0);
if (path.size() > 2) {
//Initialize temp variables for sum distance between points and
//and save the previous point
double tmp = 0;
LatLng prev = p0;
for (LatLng p : path) {
//Sum the distance
tmp += SphericalUtil.computeDistanceBetween(prev, p);
if (tmp < distance) {
//If it is less than certain value continue sum
prev = p;
continue;
} else {
//If distance is greater than certain value lets calculate
//how many meters over desired value we have and find position of point
//that will be at exact distance value
double diff = tmp - distance;
double heading = SphericalUtil.computeHeading(prev, p);
LatLng pp = SphericalUtil.computeOffsetOrigin(p, diff, heading);
//Reset sum set calculated origin as last point and add it to list
tmp = 0;
prev = pp;
res.add(pp);
continue;
}
}
//Add the last point of route
LatLng plast = path.get(path.size()-1);
res.add(plast);
}
return res;
}
}
Conclusion
You can see a result of sample code in the following screenshot
The sample project can be found on GitHub:
https://github.com/xomena-so/so47784512
Do not forget to replace an API key with your's.
I hope this helps!

Related

How to calculate distance between two marker points in android

I am creating an android app which shows distance and duration of two marker points in the Map. In the onCreate() I have written the following code:
In MapsActivity.java
private List<LatLng> getDirectionPolylines(List<RouteObject> routes){
List<LatLng> directionList = new ArrayList<LatLng>();
for(RouteObject route : routes){
List<LegsObject> legs = route.getLegs();
for(LegsObject leg : legs){
String routeDistance = leg.getDistance().getText();
String routeDuration = leg.getDuration().getText();
setRouteDistanceAndDuration(routeDistance, routeDuration);
List<StepsObject> steps = leg.getSteps();
for(StepsObject step : steps){
PolylineObject polyline = step.getPolyline();
String points = polyline.getPoints();
List<LatLng> singlePolyline = decodePoly(points);
for (LatLng direction : singlePolyline){
directionList.add(direction);
}
}
}
}
return directionList;
}
I am not clear how to calculate distance & duration in 'getText' in the code above. I was not able to see some APIs like Distancebetween() which is using LtnLtg as references.
Please suggest how to calculate the distance and duration values.
var lat = marker.getPosition().lat();
var lng = marker.getPosition().lng();

Update and delete specific marker in Google Map

I am displaying the location of buses in google map where I am getting the location from the bus database table on the server. I am facing problem to delete or to update their locations on google map since a new marker is always being created when the longitude and latitude change in the bus table. How can I delete and update specific Marker in Google Map?
I appreciate any help.
Code:
private void gotoLocation(double lat, double lng, String route_direct) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
final float zoom = 11;
LatLng ll = new LatLng(lat, lng);
if (lat != 0 && lng != 0 && !route_direct.isEmpty()) {
MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
.position(ll).visible(true);
Marker marker = map.addMarker(markerOpt);
marker.showInfoWindow();
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
map.moveCamera(update);
}
}
I solved in this way. I created HashMap for markers.
Sample,
HashMap<String, Marker> markerlist = new HashMap<>();
markerlist.put(route_direct, yourmarker);//add marker to list
markerlist.get(route_direct);//get marker from list
Then in your update process, try this code
if(markerlist.containsKey(route_direct)){
Marker marker = markerlist.get(route_direct);
//update marker
}else{
//add marker or do anything
}
but to use this flow, you need to have unique data for marker such as marker id.
Hope this will help you. Good luck.
Here we have used on our project in didTap marker delegates, We have created two functions one is Create marker and second one is delete marker code is given below in IOS swift
1.CreateMarker function call when you get Response from APIs
2.DeleteMarker call when you want to delete marker from map
//MARK: CreateMarker
func CreateMarker(TripLocation:[RECDATA])
{
//let path = GMSMutablePath()
for i in 0..<TripLocation.count
{
// Marker icon
var image = UIImage(named: "ic_greenMark")
// Get location coordinate
let locationTujuan = CLLocation(latitude: Double(TripLocation[i].facilityLatitude ?? 0.0) , longitude: Double(TripLocation[i].facilityLongitude ?? 0.0) )
image = UIImage(named: "ic_greenMark")
// create marker
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(locationTujuan.coordinate.latitude, locationTujuan.coordinate.longitude)
// marker.title = titleMarker
marker.icon = image
marker.map = MapView
let camera = GMSCameraPosition.camera(withLatitude:CLLocationDegrees(TripLocation[i].facilityLatitude!), longitude: CLLocationDegrees(TripLocation[i].facilityLongitude ?? 0.0), zoom: 8)
MapView.camera = camera
// append marker into markers array to show all marker in map
markers.append(marker)
}
}
//MARK: DeleteMarker
func DeleteMarker(marker:GMSMarker)
{
// Create Temp Object array
var Tempmarkers = [RECDATA]()
//check marker is exist or not
for obj in arrayRECDATA
{
let lat = Double(obj.facilityLatitude!)
let log = Double(obj.facilityLongitude!)
// remove marker from object array
if marker.position.latitude != lat && marker.position.longitude != log
{
Tempmarkers.append(obj)
}
}
// store temp array into original array
arrayRECDATA = Tempmarkers
// clean all marker and reload
MapView.clear()
CreateMarker(TripLocation: arrayRECDATA)
}
Thank you :)
Define variable for each marker like
Marker m1;
Marker m2;
etc.
And do whatever operation on specific marker using that variable. To delete the specific marker
m1.delete ();
m2.delete ();
Something like this, you can try and let me know if you face any issue.

Can i add marker clustering to my already set map and markers?

so i already have a map set up and i have added all markers with icons and title using a function addMarkersToMap() but they are so sparse so i was thinking of clustering them. Is there a way for me to cluster the markers that i have already set up on my map? I found this code which is awesome for clustering from a .json file but my markers are already set up on the map and i don't know how to cluster markers with already set up icon and title.
protected void startDemo() {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));
mClusterManager = new ClusterManager<MyItem>(this,map);
map.setOnCameraChangeListener(mClusterManager);
try {
readItems();
} catch (JSONException e) {
Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
}
}
private void readItems() throws JSONException {
InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
List<MyItem> items = new MyItemReader().read(inputStream);
for (int i = 0; i < 5; i++) {
double offset = i / 60d;
for (MyItem item : items) {
LatLng position = item.getPosition();
double lat = position.latitude + offset;
double lng = position.longitude + offset;
MyItem offsetItem = new MyItem(lat, lng);
mClusterManager.addItem(offsetItem);
}
}
You cannot use cluster as well as your already setup custom marker (with Title and Snippet) together.
Once you start using cluster, all marker addition and related tasks will be taken care of by the clustermanager.
But all hope is not lost !!
You can add the POJO variables, their getters and setters inside the MarkerItem object you created for the cluster manager.
You can then create a class like this inside your map activity itself,
class OwnIconRendered extends DefaultClusterRenderer<MyItem> {
public OwnIconRendered(Context context, GoogleMap map,
ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
//markerOptions.icon(item.getIcon());
markerOptions.snippet(item.getContact());
markerOptions.title(item.getName());
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
and set this render for your clustermanager like this
mClusterManager.setRenderer(new OwnIconRendered(activity.getApplicationContext(), getMap(), mClusterManager));
For more detailed explanation you can refer this answer which I have referred. http://stackoverflow.com/questions/27745299/how-to-add-title-snippet-and-icon-to-clusteritem

Android GoogleMaps v2: error when adding multiple markers using 'for' loop

I need to add a given number of markers to a map using a for loop. The Log messages tell me the function to add each marker is called, but only one marker (two at most) are displayed on the map. My two functions are the following:
private void paintInMap(String description){
map.clear(); // to erase previous markers
String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
String[] route = description.split(", "); // split the different places of the route description
for(int i=0; i<route.length; i++){
for(int j=0; j<zones.length; j++{
if(route[i].equals(zones[j])){
LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
placeMarker(latLng, zones[j]);
}
}
}
}
and:
private void placeMarker(LatLng coordinates, String name){
map.addMarker(new MarkerOptions()
.title(name)
.icon(BitMapDescriptorFactory.fromResource(R.drawable.gpsmap))
.position(coordinates)
.flat(true)
.rotation(90));
Log.d("PLACE", name+" added to map");
}
Apparently my code is correct, but on runtime it only displays one (or two) markers. I have checked the Log messages and the function is being called, but the markers do no appear. Moreover, one of the markers appears in an unspecified location (which corresponds to the first value of the coordinates array by the way)
Is this a runtime bug in Eclipse? How can I solve this?
map.clear(); // to erase previous markers
new AsyncTask<String, MarkerOptions, Void>() {
private void placeMarker(LatLng coordinates, String name) {
publishProgress(new MarkerOptions()
.title(name)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.gpsmap))
.position(coordinates)
.flat(true)
.rotation(90));
Log.d("PLACE", name + " added to map");
}
#Override
protected Void doInBackground(String... params) {
String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
String[] route = params[0].split(", "); // split the different places of the route description
for (int i=0; i<route.length; i++) {
for (int j=0; j<zones.length; j++) {
if (route[i].equals(zones[j])) {
LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
placeMarker(latLng, zones[j]);
}
}
}
return null;
}
#Override
protected void onProgressUpdate(MarkerOptions... markers) {
map.addMarker(markers[0]);
}
}.execute(description);
I finally solved it by running the for loop in the UI thread instead of doing it using an AsyncTask
......
route = descriptionRoute.split(", ");
coordinates = getCoordinates(coord);
String[] zonas = getResources().getStringArray(R.array.array_zonas_madrid);
String[] coord = getResources().getStringArray(R.array.array_coordinates);
for(int i=0; i<route.length; i++){
for(int j=0; j<zonas.length; j++){
if(route[i].equals(zonas[j])){
LatLng latLng = getCoordinates(coord[j]);
placeMarker(latLng, zonas[j]);
}
}
}
....

Unable to see multiple marker on google Map

I want to show multiple markers on a google map. My latlng coordinates are fetched from a Parse database but I am not able see marker.
My second problem is that I want to show a title that is Restaurant Name with marker, how can I do this?
This is my code:
private class putMarker extends AsyncTask> {
#Override
protected ArrayList doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
Toast.makeText(getApplicationContext(),
longitude + " " + latitude, Toast.LENGTH_SHORT).show();
ParseQuery query = new ParseQuery(
"Details");
ParseGeoPoint myGeoPiont = new ParseGeoPoint(latitude,
longitude);
query.whereNear("location", myGeoPiont);
query.setLimit(10);
ob = query.find();
for (ParseObject resObj : ob) {
ParseGeoPoint location = resObj
.getParseGeoPoint("location");
restaurantName = (String) resObj.get("RestaurantName");
LatLng resLatLng = new LatLng(location.getLatitude(),
location.getLongitude());
Toast.makeText(getApplicationContext(),
restaurantName, Toast.LENGTH_SHORT)
.show();
PiontList.add(resLatLng);
}
} catch (Exception e) {
// TODO: handle exception
}
return PiontList;
}
protected void onPostExecute(ArrayList latlngList) {
for(LatLng res: latlngList)
{
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(res);
markerOptions.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(markerOptions);
}
}
}
Please help me out.
it might due to unreachability of googleMap object in onPostExecute() .
Please ensure that googleMap is declared globally.
if possible please paste whole code for better evaluation
Try this
// Create lat long points
Latlng[] point_new = new LatLng[8];
point_new[0] = new LatLng(31.5301843, 74.3207487);
point_new[1] = new LatLng(31.5214693,74.3236027);
point_new[2] = new LatLng(31.5194393, 74.3257327);
point_new[3] = new LatLng(31.4942166, 74.3004533);
point_new[4] = new LatLng(31.4864646, 74.2911203);
point_new[5] = new LatLng(31.4803596, 74.2787933);
point_new[6] = new LatLng(31.4764716, 74.2638203);
point_new[7] = new LatLng(31.4775236, 74.2628873);
// Add markers
for (int i = 0; i < point_new.length; i++) {
MarkerOptions markerOptions = new MarkerOptions()
.position(point_new[i]);
marker = mMap.addMarker(markerOptions);
marker.setTitle("Points");
marker.setSnippet("Distance = 9.6 km, Time = 20 minute/s");
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.p));
}
// Set camera to last point with Zoom level 9
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point_new[7], 9));
I see a few problems here.
As #Raghunandan mentioned, you cannot update the UI from doInBackground() so you cannot add markers from there. You can however, make your MarkerOptions objects here, and then attach them to the GoogleMap in your postExecute/or in the Activity that hosts the Google Maps.
In your onPostExecute(), you have not set any Title, or Snippet to your markers. Whenever you are creating your marker, make sure to set your title. Then when the user clicks on the marker, the default behavior shows your rest name as a title.
Code will be something like this(as also mentioned by #Inzimam Tariq IT :
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(res)
.setTitle(restaurantName)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(markerOptions);

Categories

Resources