How to save markers? - javascript

I have an app that saves google map markers, I basically save the lat lng coordinates/vriables in an array, then I save it in my mongodb. When I reload those markers, I loop in the array and create markers from the saved lat lng variable.
My problem is that when I saved my markers the first time, a lat was called Pa and a lng Oa, then 1 month ago, I save markers, and then try to load them, but my loop doesn't recognize the lat lng variables, they were changed to Sa, Ta. So I changed my client code to pull variables of Sa, Ta instead, no big deal.
Today, I save another array of marker, try to load it, and it doesn't work, again their native google api variable name are changed to lat Ua and lng Va ... My client code doesn't work for any new saved marker.
I'm obviously not saving markers the "google" way, what am I doing wrong?
Thank you

You should not rely on the variable names of minimized code. Call them "lat" and "lon" or something like that. The internal names such as Pa are potentially lost on each recompilation. In particular, they will likely change when Google fixes a bug or releases a new version of the API.
So don't just blindly encode the marker objects. Convert them to a stable representation that you control yourself (and not the automatic code compiler of Google maintains and changes at will).

Since the API seems to be changing every once in a while you might want to decouple your implementation from it. Store lat and lon however you want and then write adapter that maps that data to Google API suitable and back on save.

mongodb is unsuitable for storing lat/lng as there is no arbitrary precision or decimal data type in BSON. mongo data types

Related

MapBox GL JS: Get lat and long coordinates from string Address that is set through code

I am using Mapbox GL JS to show certain location marked on the map.
I need to set location through JavaScript code and I don't know the lat and long coordinates, I only have an address in string form (example: "address, city, country" or some similar format).
I have searched a lot on the internet, but the only way that I have found for doing this in Mapbox GL JS is by using Geocoder input control, but I don't want to input location through it, I want to set Address through code and then have it marked on map when the map loads.
Is it possible to do this in Mapbox GL without knowing lat and long coordinates? How can I do that?
Thanks in advance! Best regards!
You can use the Mapbox Geocoding API without the Geocoder control. You have to compose your url (and encode it).
Always consider you could get more than one result in the featureCollection response depending your params, so finally you have to refine the results with some of the attributes allowed such as proximity and limit. At the beginning is a bit of try and error task, but after some practice it's quite accurate.
Here you have an example url for the address 123 Main St Boston MA 02111, filtering the results in United States only.
https://api.mapbox.com/geocoding/v5/mapbox.places/123%20Main%20St%20Boston%20MA.json?country=US&access_token=pk.eyJ1IjoianNjYXN0cm8iLCJhIjoiY2s2YzB6Z25kMDVhejNrbXNpcmtjNGtpbiJ9.28ynPf1Y5Q8EyB_moOHylw
This will return 5 streets in the Massachusetts state.
I recommend you to read the documentation in forward geocoding, and also the section on address geocoding format to compose properly the url.

How to get postal code from current location

I'm doing restaurant search engine for my project and I'm stucked with current location.. I can get my lat and lng from navigator (geolocation HTML5).
The point is, that I need postal code, not lat and lng. The only idea I had was to get that lat and lng and connect with postal code using some API.
I've found something like this:
http://api.geonames.org/findNearbyPostalCodes?lat=52.198500&lng=-2.219200&maxRows=1&username=demo
How can I refer to the post code from that API? The best with javascript?
Have you got maybe other ideas? I've got one button and after clicking on that button, I want postcode from my location in text input.
Would you be able to make an API to the following Service and convert the data?
Please look at the following and let me know if you need help Consuming the API..
https://docs.mapbox.com/api/search/#geocoding

Get cities within radius from a location (Google Map API)

I'm doing a little project which heavily relies on locations and my question here is...
Is it possible to use the google map API to get cities within a certain radius from a location so that I can reduce the number of times I have to query on my database before I get the results I want?
I have previously done a radius search by using the locations stored on my database and I'm thinking that if it is not possible to get the cities within radius with just the use of the google map API,
My alternative solution would be to get all locations (stored in my database) within a certain radius by filtering them via lat and lng and get all distinct localities of the returned locations.
However, results can be inaccurate. Because there can be datas stored up in my database that might not show up if it is not within radius from the location specified during radius search, despite being in the same city/locality, which is actually the filter that I want.

Google map api - How to Get the Place's Name from lat/lng

Greetings i was searching for hours and I haven't found anything remotely close to this.
I am trying to use Distance Matrix API, However as an input the api needs the Origin Address aka Name and Destination as well. But i can only provide Origin's Lat/Lng and Destination's Lat/Lng from which i get them using my own markers.
Is it possible to get the place's Name and Address from Lat/Lng of the given origin?
Or Am i in whole wrong path to achieve this? any suggestion would be appreciated. thank you.
For your information. DistanceMatric Api works not only with string addresses it also works with lat/lng, place_id as well.
Go through below doc
https://developers.google.com/maps/documentation/distance-matrix/intro
So, no need to convert your lat,lng to string addresses.(otherwise simply api hits will increase)
If you want to see result. Check below api.
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592&key=[API_KEY]
Note: Replace API_KEY with your api key
You can use the latitude/longitude coordinates for origin and destination parameters. Its in the documentation https://developers.google.com/maps/documentation/distance-matrix/intro
But if you really want to get the address from latlng you can reverse geocode it like this:
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
here is the link to the documentation for your reference
https://developers.google.com/maps/documentation/geocoding/start

Unlimited Markers on Google map without query Limit using Geocoder

I have a google map with multiple markers.I done that using google map Geocoder with the help of this link Geocoder usage .But,it was limited to some markers only.After the limit it is showing the error as queryLimit.Is there any way to show the multiple markers on the map without any limit.It can be other than Geocoder too.Thanks
You should never use a webservice or API to get the locations every time you want to show the markers. What you need to do is store the latitude and longitude somewhere. Preferably the same place you are storing the address information. Then just use those latitudes and longitudes to generate the markers with no need to worry about query limits.
The reason you are exceeding the query limit is that you are doing the queries too fast. If you have a period between each query you can do a lot more before hitting the querying cap, but this is not a solution. You should always save the location instead of querying every time :)

Categories

Resources