This question already has answers here:
Google Maps API Geocode Synchronously
(3 answers)
Closed 1 year ago.
I need to put Info Window on markers in a Google Map. I make an Ajax query to get a list of markers to draw in my map and I have a sync problem because the:
geocoder.geocode( { 'address': citta}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {}
Is asynchronous so if I call it inside a loop I get the wrong results. Can anyone tell me if there is a way to do a synchronous call?
See the following answer Google Maps API Geocode Synchronously. The answer is no.
Related
I have seen that it is possible with the "static" to limit the scope of the search to a certain area (with components restrictions), I have also seen that in release 3.exp (will be 3.14) of the JavaScript API, a new class google.maps.GeocoderComponentRestrictions has appeared. I have not seen anywhere how this class is supposed to be used though.
Does anyone have more information about this?
It looks like this was answered over at Google Maps Geocoding API, feature from the API missing in their JS api (?), with formatting like:
geocoder.geocode(
{ 'address': address, 'componentRestrictions':{'country':'GB'}},
function(results, status){
...
});
The GeocoderComponentRestrictions parameters get passed with the componentRestrictions key.
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();
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())
I want to find out list of all bus stops between two places using google map api.
I have all transit feeds , but i don't know how to work with that.
can anyone tell me the step by step process to work with google transit?
Thanks in advance..
There's a Google Transit official API, you can read about this here:
https://developers.google.com/maps/documentation/directions/#TravelModes
https://developers.google.com/maps/documentation/javascript/directions#TransitOptions
https://developers.google.com/maps/documentation/javascript/directions#TransitInformation
You must create a route for getting to this information:
directionsService.route({ 'origin': initial_pos, 'destination': final_pos, 'travelMode':google.maps.TravelMode.TRANSIT}, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
//Setting the map to display
directionsDisplay.setMap(map);
//Setting the direction panel
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
//Setting the transit layer if you need
var transitLayer = new google.maps.TransitLayer();
transitLayer.setMap(map);
//Setting the directions response
directionsDisplay.setDirections(response);
});
Google transit is not a part of google maps API and has no api of itself.
You could add request param output=json to the end of google transit request link and parse json, or even try parsing HTML itself.
Let's say I have a LatLng object, is there any way of checking if it represents a possible location within a city? How can I get the limits of a city?
I'm using Google Map V3.
Have you tried reverse geocoding?
http://code.google.com/apis/maps/documentation/javascript/services.html#ReverseGeocoding
You could check the formatted address to see if the city matches what you're looking for. I'm not sure how accurate this will be for your application, but it's an option.
function codeLatLng() {
var geocoder = new google.maps.Geocoder(),
latlng = new google.maps.LatLng(40.730885, -73.997383);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
console.log(results[1]);
console.log(results[1].formatted_address);
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
City limits and other municipalities are constantly re-drawn. There are certain services that exist to help you find them, but I'm not positive that Google keeps a record of city limits inside their data for the Google maps. Here's a discussion in google groups about it. A snippet from that site:
Depending where you are in the world, city limits and other
administrative boundaries are CONSTANTLY being changed, and it's even
sometimes difficult for local governments to keep their data current
because of annexations and other changes. Also, 'city' might be
something relative small or the size of Shanghai. Also, different
countries can also have sometimes conflicting definitions what
administrative unit is the actual definition - a good example is
China. Probably your best approach is to get your data from a local
government or a data supplier and build your own.
You can use Reverse Geocoding in the Google Geocoder API and check the locality entries and perhaps sublocality entries of results returned. http://code.google.com/apis/maps/documentation/geocoding/#Types
Note that this probably won't work so well if you have addresses all over the world and want to know "What city is this LatLng in?" On the other hand, it will probably work reasonably well if you want to know "Is this LatLng within Chicago?" There are areas of the world where the data is fairly complete and sensible, and areas where it is incomplete and/or organized in ways you might not expect. (Apparently, the UK uses "county" and "state" very differently from the USA, for example. Even if I'm wrong about that, you get the idea.)