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!
Related
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
I want to show map only one city or country in my android app. How can I do it ?
I do not need to load map of all world
image 1
image 2
if you want to see only country or city you can use its latlng bound its focus on the particular city or country when user come first time here all country latlng bound https://gist.github.com/graydon/11198540
First, you have to be familiar with Google Map API (create an API key, import it to your project, etc.)
Then, get the LatLng of the city you want, and on the moveCamera method, pass to it a good zooming (it's a float). For exemple, when your widget is beeing launched, call something like : mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(yourCoords, 10f))
If you don't want / need to allow any user interaction with your map, just set the parameter "liteMode" to true inside your XML (or programatically), it will generate a bitmap of your desired location.
Bye.
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.
So I am trying to build an app that would allows the user to select a particular place from a Google Maps intent, and when the user pick a place, the intent would finish and return the latitude and longitude data to the previous activity.
What is the best way to do this?
What I have done so far:
I used GPSTracker from here.
to get the current latitude and longitude, and then used these data to start a google map intent pointing to current location.
The website you posted in your question does not look like it contains the actual map. If you need assistance with adding a Google Map to your application I would start here.
Assuming you have that working and your code allows a user to place his or her point already, you can retrieve the coordinates of that point by using a built in function in the Marker class getPosition().
You can use the coordinates by accessing the public variables .latitude and .longitude as follows:
LatLng coord = myMarker.getPosition();
double latitude = coord.latitude;
double longitude = coord.longitude;
I have an activity that displays a list on markers based on the state of a model.
Whenever the model's state is changed, the markers are refreshed to display the new location or display any new markers.
I want to test this behavior but GoogleMap does not provide a .getMarkers() method or similar to know which markers are shown on the map.
The question is, how can I test both the number of Markers and the LatLng or each marker.
keep an ArrayList of all plotted markers and iterate through to find the one you want?
or there is a 3rd party mapping library that you can call getMarkers, getPolygons etc..
Thats just an idea but you can store you marker (latlng, id, image, etc) on an ArrayList. Then when you create your marker, assign foreach the id and on click retrieve the id to get all information's from your Array.