Android marker ID in google map - android

I am planning to get the marker ID and want to show some information based on marker id. I am using the following code to get marker id:
Log.d("marker", marker.getId());
I have a seperate line where I want to associate marker id with other tasks, however, for some reason it doesn't show any values/execute that section.
if (marker.getId().toString() == "m1") { .... }
any help would be highly appreciated

that is not how you equate a string in java.
marker.getId().equals("m1") is what you want since getId is already a String you dont need to do toString()

Related

is it possible to remove android Google Maps Marker based on Tag?

I am attaching a tag to a Marker when I add it to GoogleMap like this:
val icon = IconGenerator(context).makeIcon("${listing.cost}$")
map.addMarker(
MarkerOptions().position(LatLng(address.latitude, address.longitude))
.icon(BitmapDescriptorFactory.fromBitmap(icon!!))
).tag = listing
Is it possible to remove the marker later if it matches the tag I assigned it to?
Listing is an object on the Database that I am adding to GoogleMap, and I want to remove the listing Marker when it is removed from the Database. Thanks.
I believe the answer is No. I ended up saving the Marker in a ViewModel hashMap variable that has Marker id as key, and actual Marker Object as Value.
I stored the Marker's id on a database then I get the actual Marker from the hashMap using the id as key, then remove it.

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

load map only single city on google map android

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.

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!

Applying adapter concept in google maps Marker android

I populate my google maps with number of Markers based on my server data.
MY server data contains ProfilePic , Name , Designation , & lat long etc...
When I click on my Marker I open BottomSheet that display the details like ProfilePic , Name , Designation . just like google map does
I don't know how do I get ProfilePic and other data on click of my marker .
I am very familiar to Recyclerview adapter concept ,
How do I apply this concept to my marker ?
You can set custom adapters of infowindow for marker in google map.. check this example--http://androidfreakers.blogspot.in/2013/08/display-custom-info-window-with.html
Solution 1-> You can use HashMap for mapping of markers and your server data list.
Solution 2-> You can append the id or position to marker title with token(comma, semicolon...) and then onClick marker you can break them for further mapping to your server data list.
In this case you also need to override customInfoWindow.
Example to follow:-
http://devlearning.blogspot.in/2014/05/android-custom-info-window-with.html
Hope this may helpful to you
Thanks,
Bhuvnesh

Categories

Resources