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
Related
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.
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!
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.
I have a problem with lonitude and latitude in google maps.
This is way how I add my markers:
Marker newStore = map.addMarker(new MarkerOptions().position(new LatLng(store.getLatitude(), store.getLongitude()))
.title(store.getName()).icon(BitmapDescriptorFactory.defaultMarker(markerResolver("up"))));
where store.getLatitude = 44.04687 and store.getLongitude = -70.295734.
When I print marker position after add them this is what I got:
marker.getPosition().latitude = 44.046873756 and marker.getPosition().longitude = -70.295734543234.
Now I want to equal both data to search my store by position, but I can't do that, because store position is different then marker position. Markers position is more precise then sote position. How can I avoid that. I ask that because I have a list of stores and add markers by position. Now I want to get which store I click by position but as you can see position of marker and sotre is different.
You should not compare Markers or any other objects by LatLng position, because of this bug.
You may instead keep a Map<Marker, Store> with all your Markers and Stores and retrieve it like this in onMarkerClick:
Store store = map.get(marker);
More on that and other approaches here: https://stackoverflow.com/a/17000070/2183804
One option is to leverage the concept of Geohashing to evaluate equality based on proximity between two points. Geohashes sizes (distance) can be configured upon instantiation. So in your example you could construct to Geohashes like so:
GeoHash myLocHash = GeoHash.withCharacterPrecision(store.getLatitude(), store.getLongitude(), 7);
GeoHash markerLocHash = GeoHash.withCharacterPrecision(marker.getPosition().latitude, marker.getPosition().longitude, 7);
if (myLocHash.equals(markerLocHash)) {
// Same location +/- .61 km margin of error
}
This is probably overkill for what you need, but can be a really handy option for other applications. Here is a great Java library for Geohashes:
https://github.com/kungfoo/geohash-java
More reading on Geohashes:
http://en.wikipedia.org/wiki/Geohash
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.