Android: get nearest street from marker point - android

Am adding markers in my android app, but I need to make one feature, if I add marker not on street, I need marker to be added on the nearest known street. Like in this example: Add marker to nearest known street, but I need to make it with android.
this is my marker coordinates:
MarkerOptions marker = new MarkerOptions().position(
markerLatLng = new LatLng(point.latitude, point.longitude)).title("my marker");
Now I need execute async task to send coordinates to google and retrieve nearest road coordinates.What url should I use for that?
Then am planning to add coordinates to arrayList:
locationArray.add(new LatLng(point.latitude, point.longitude));
And show marker on my map:
mGoogleMap.addMarker(marker);
I enabled in my API manager : Google Maps Directions API and Google Maps Roads API
The question is ,what URL in my AsynTask should I use to get nearest street coordinates?
Maybe I need to use this: https://maps.googleapis.com/maps/api/directions/output?parameters and add my marker coordinates to parameters?

You know how to use json i recommend the following:
https://maps.googleapis.com/maps/api/geocode/json?latlng=yourLat,yourLng
In the response you will have all information about that LatLng, nearest street and number, city, states, country and plenty more.

If your purpose is to snap the coordinate to the nearest road you should consider the Roads API that has a Nearest roads functionality.
Have a look at the documentation
https://developers.google.com/maps/documentation/roads/nearest
So the URL that you can use in the async task is something like https://roads.googleapis.com/v1/nearestRoads?parameters&key=YOUR_API_KEY
If your purpose is to find the nearest available street address the Reverse Geocoding is good for this as was suggested by Henrique.

Related

Limit Marker within city limit android Google Maps

I have an requirement, in which Marker should be placed on clicking the Map within the city limit in Google Maps android.If user clicks on the map outside the city limit. Need to show an warning. I know it is possible to add marker on clicking on the map but how can i restrict it to city limit? I tried exploring the concept of GeoJson to add an layer above my Map.But I am not sure how this would help. Any suggestion appreciated. Thanks
Based on this link I could able to fetch polygons for a particular city. The values are stored to DB and fetched using an API. API returns lat lng boundary of a city, Using this I could able to draw an polygon in shape of City.
val polygon = googleMaps?.addPolygon(PolygonOptions()
.addAll(cityLimit)) // cityLimit LatLng list of City Boundary
polygon?.isClickable = false
polygon?.strokeWidth = 2f
Then onMapClick, I added the following condition
override fun onMapClick(latLng: LatLng?) {
if (!PolyUtil.containsLocation(latLng, cityLimit, false))
// Show error message "Unable to place marker outside city limit"
}
PolyUtil is an Class library of Google Maps sdk. This solved my problem. For more details check this blog
You should create a Geofence around the area you want the user to select. Then you can evaluate whether it is in the city boundaries.
Have a geofence like 10000m around the city,if the user clicks the map and find that it is not inside the geofence you cancel the request but if it is within the boundaries you create a marker

Getting the place name from a marker on Google Map

I am displaying Google map in my fragment with markers.
I want to retrieve place name from marker placed(I am not setting title to the marker).
In the below screenshot I want to retrieve marker with name (Here Pu. L. Deshpande Vanodhyan) As I want to display that name in another view.
I dont want to use Places Api.
It's not possible to get the marker's position's name with the Maps SDK for Android alone. To get the name, you need to use another service such as the Places SDK for Android or the Geocoding API web service.
If you go for the latter, then I recommend you first call this method:
LatLng position = marker.getPosition(); // returns a LatLng object
Then add the value to the (Reverse) Geocoding request as follows:
String geoRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + position + "&key=YOUR_API_KEY";
Which should give you results with a formatted_address for that LatLng, e.g. the first result for "40.714224,-73.961452" is "279 Bedford Ave, Brooklyn, NY 11211, USA".
Hope this helps!

Dropping the location pin on a place given the name/Google Maps URL?

I have a MapView in my app, and I can load it without problems, with the Location Pin Dropped on a certain longitude and latitude.
Here is the code I used:
private MapView mapView;
private GoogleMap gmap;
gmap = googleMap;
gmap.setMinZoomPreference(17);
UiSettings uiSettings = gmap.getUiSettings();
uiSettings.setCompassEnabled(true);
uiSettings.setZoomControlsEnabled(true);
LatLng ny = new LatLng(40.7143528, -74.0059731);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(ny);
gmap.addMarker(markerOptions);
gmap.moveCamera(CameraUpdateFactory.newLatLng(ny));
It works well. The map loads and the gesture and enabled. The pin is dropped at the specified latitude and longitude as well.
However, I'd want to change this. I want the pin to be dropped based on the name of a place (Empire State Building, for example), or based on the URL (http://maps.google.com/?q=Empire%20State%20Building). However, upon spending time going around the internet and the documentation, it seems that the only way to set a pin is through a LatLng Object.
While I have no strong objections about using Lat and Long, I would prefer using the Identifiable Name or the URL of the place itself, as it would make the database easier to read.
Has anyone successfully set the pin using either the name or the Google Maps url?
One solution I can see is that to convert the name of a place to a lat-long using Geocoding API.
Using Geocoding we will be able to convert address (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739), and you can use that information to place the markers on map.

get google maps data with retrofit 2

I try to get the distance between two points by showing them together with a polyline. One marker is permanently somewhere and the other the user chooses the place they want when clicking on the map. I tried with many codes obtained from the Internet but for me neither works or just gives me the marker of a place with the latutude and longitude. I want to use retrofit 2 to bring the information. Can anyone help me with that?
On clicking on map you are placing marker, So if you want to get location of that marker then simply you can got that marker position using that marker object like this.
Double latitude = marker.getPosition().latitude;
Double longitude = marker.getPosition.longitude;
and you have already one location so you can find distance between two locations using this.
locationFrom.distanceTo(locationDestination);
here locationFrom is your source location from where you want to find distance and locationDestination is your destination location to where you find distance.

Google Map Checking if cooridinate is between 2 points

ANDROID APP
I've use google direction API to get a route from 2 points.
All the routes returned is saved if there is a need to reference to any of that data
Then I decoded the overview_polyline into a list of LatLng and drew the polyline on the google map.
How do I find out if a given coordinate (traffic accident for example) is on/nearby the polyline route , plus minus a constant distance (maybe 100m?)
You can use Google Maps Utility Library.
Especially isLocationOnPath(LatLng point, java.util.List<LatLng> polyline, boolean geodesic, double tolerance).
Here's the full documentation.

Categories

Resources