How to properly use Google Maps API? [duplicate] - google-maps-api-3

This question already has an answer here:
google.maps.Geocoder.geocode() geometry.location lat/lng property names change frequently
(1 answer)
Closed 9 years ago.
I found out, while using Google Maps API, the results change from time to time. By change I mean the response object. Basically I'm querying Google Maps to get a list of places based on user input (also using jQueryUI auto complete). I'm trying to correctly fetch the coordinates which get returned from Google Maps API but the properties containing the exact|approximate coordinates seem to change from time to time.
For example: in earlier days I was able to get coordinates like item.geocode.geometry.location.Ya and item.geocode.geometry.location.Za. Then my search form broke because Google changed it to item.geometry.location.mb and item.geometry.location.nb.
Any suggestions on how to implement this the correct, and stable way?

Never use undocumented properties of a Maps API object like the ones you found. Those will change wildly whenever the Maps API is revised.
Instead, use only documented methods and properties.
Your item.geocode.geometry.location is a LatLng, so you can call its .lat() and .lng() methods:
var location = item.geocode.geometry.location;
var lat = location.lat();
var lng = location.lng();

Related

google api geocoding returns json object different than official docs

I've been successfully using the google api for geocoding, but when I originally set it up I noticed that the object returned is not quite the same as described in the google documentation here: https://developers.google.com/maps/documentation/geocoding/#GeocodingResponses
I used console.dir to inspect the object in chrome.
Unless I've misunderstood (quite likely), I can retrieve the latitude and longitude for the supplied address as follows:
lat = results[0].geometry.location.lat;
lng = results[0].geometry.location.lng;
Except, that wasn't working, and when I investigated I found that lat and lng were at results[0].geometry.location.d and results[0].geometry.location.e respectively.
All good until today it stopped working. Investigating I found that lat & lng are being returned via results[0].geometry.location.k & results[0].location.A.
Presumably this will break again before too long.
If I inspect results[0].geometry.location.lat it contains this:
function (){
\"use strict\";
return this[a]}
Anyone recognise that?
The results returned from the geocode-method of the Javascript-Geocoder is already translated to be used with the Javascript-API(the linked documentation is for the Geocoding-Webservice, not for the Maps-Javascript-API).
geometry.location is not a plain object, it's a google.maps.LatLng
you must use the methods lat() and lng() of geometry.location to retrieve latitude and longitude.
Specification of GeocoderGeometry
In your case that would mean using it like this:
lat = results[0].geometry.location.lat(); etc.

Google map how to display a dragable marker based on coordinates and return new coordinates

I have to write a script for Google Maps V3 which will do the following:
Place a marker on a map based on a set of coordinates and possbibly change the address after the page has been loaded
Allow the user the drag the mark to replace it elsewhere
Collect the new coordinates once the marker has been moved.
The first part is basic enough and should be fine, changing the address after the page is loaded should only be a matter of calling back the same function?
I am not sure where to start on the draggable marker and collecting hew coordinates.
Would anyone know of scripts / API's or where in the doc should I start?
Have you checked the Google Maps Javascript API V3 Reference to see whether the google.maps.Marker class has :
a draggable option
an dragend event
a getPosition method
Hint: The answer might just be "yes" to all three, and you don't need much else to solve your problem.

Store Locator API + Geolocation

I am trying to create a Store Locator with the Google API, very similar to the one in the Google examples here:
http://storelocator.googlecode.com/git/examples/panel.html
However I've hit a wall trying to get the Store Locator API to get the user's position through Geolocation, so when I click on Get Direction in the infowindow I get directions to the user position; instead of having to type my address in the: Where are you? Panel box.
In the documentation what I have seen is that geolocation is a boolean in the View Option that is set to True by default. But this does not solve my problem.
Does anyone have any idea on how to do this?
seems that the googlecode page you said is no longer exist. So I can't give any further insight about what you want to make.
However, by your description, Luckily I made similar site few months ago. It is on Grocery Store Near Me .
The concept of Geolocation is an HTML5 (actually W3C) Geolocation API which is now already embed in most modern browser. It is an API which you can obtain user's location (latitude, longitude, altitude, and accuracy).
you can call it with simply
navigator.geolocation.getCurrentPosition(success, error, geo_options);
Success and error is a callback function in which you can define.
In my case, the function looks like these
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// use the lat and long to call function to fetch an API through AJAX
fetchStoreData(latitude, longitude);
}
In the Store Locator schema (like mine in Grocery Store Near Me), Geolocation is used to obtains user lat and long. The lat and long, then send to server via AJAX to get data about nearby store location.
The server serve an API in which accept lat and long as parameter, then fetch the store data (either using database, or other external API like foursquare), then you can display it either on list, or on a maps.

Google Maps API V3 - only showing a blank map when using the geocoder [duplicate]

This question already has an answer here:
'item.geometry.location.kb' and 'item.geometry.location.jb' returning undefined
(1 answer)
Closed 3 months ago.
So I have some javascript that uses the google maps API V3 and uses the geocoder to obtain co ordinates from an address and display a map at those coordinates. It was working totally fine, but then I check it again a few days ago, and now instead of showing the proper map, it just shows a blank blue map, and doesnt let you zoom in or out or move the map. There doesnt appear to be any javascript errors or errors returning from google maps, so not really sure what could be causing this...
Heres an example of the problem: Click Here
Anyone have any ideas on what might be causing this?
You are using undocumented properties:
var center = results[0].geometry.location.Xa - 0.0062;
var centerCoord = new google.maps.LatLng(center, results[0].geometry.location.Ya);
(geometry.Xa, geometry.Ya)
That is guaranteed to fail at some point when the version of the API changes.
Use the documented properties (geometry.lat(), geometry.lng())

Acquired the building name using GPS Coordinates

I am writing an app to use GPS coordinates obtained by the cell phone itself to retrieve the building name of that location.
For example, if I use this http URL to request with Google Place API:
https://maps.googleapis.com/maps/api/place/search/json?location=40.805112,-73.960349&radius=10&sensor=false&key="YourKey"
I can only get the street name of this coordinate through this.
But if I type "40.805112,-73.960349" in maps.google.com. I can get the exact building name. SO I was wondering how can I use Google Map API to obtain the building name I want.
Thank you very much about this!!!
The first result in the request you provided contains "name" : "Church of Notre Dame", isn't this what you are looking for?
A better request if you are only interested in the place name at this location would be to use the rankby=distance parameter instead or radius and to filter by type=establishment:
https://maps.googleapis.com/maps/api/place/search/json?location=40.805112,-73.960349&rankby=distance&types=establishment&sensor=false&key=YOUR_API_KEY
This would return the closest place at the given location.

Resources