I already searched in a lot of websites for a solution but no one of them worked.
Since hours I am trying to get the name of the city by using the latitude and longitude values which I will get from a input box to my x, y variables but no example worked.
I also read in the Google maps API but it was not useful.
I already have an API key.
Do you maybe have any solution?
This example I got from the website and tried but without success:
function myFunction() { var x='xxxxx'; var y='xxxxx'; //my coordinates
var geocoder = new google.maps.Geocoder;
var latlng = {lat: parseFloat(x), lng: parseFloat(y)};
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
// ...
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
Look out for Geo location API
Sample Request
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false
I am using the Google API (Javascript v3) and need to be able to convert latitude and longitude to a place name (not an address), and visa versa. I have read a lot of articles saying to use the Reverse Geocoding to get this but so far I have only been able to get the formatted_address and not the actual place name.
The code I was using for this is:
function showLocationAddress(e) {
var latlng = { lat: e.latLng.lat(), lng: e.latLng.lng() };
var geocoder = new google.maps.Geocoder;
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
$('#location').empty();
$('#location').val(results[0].formatted_address);
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
Currently the user can use the autosearch function which within a text field on the map and I can get the place_id, address, and lat and long (as discribed in this link). This is instantiated by calling var autocomplete = new google.maps.places.Autocomplete(input); and the place details are then retrieved using place = autocomplete.getPlace();.
The difficulty I am having is that I also need the same information (place_id, address, and lat and long) to be available when the user clicks on the map, but as I am not using the Autocomplete text field I don't know how to do this.
I thought it would have been something like var place = new google.maps.places.Place(latLng);.
how can I get the formatted_address in Google Maps API with neighborhood type? I'm using Google Maps for reverse geocoding but unfortunately the closest address to my location is the formatted_address with a neighborhood type. Which very hard for me to parse the JSON array since it has many content.
Here's the sample JSON that I got from Google Maps .
https://gist.github.com/anonymous/8d5250cfc37a8cef9ab2
when I try to use this:
var geocoder = new google.maps.Geocoder();
var point = new google.maps.LatLng(localStorage.latitude, localStorage.longitude);
geocoder.geocode({ 'latLng': point }, function (results, status) {
if (status !== google.maps.GeocoderStatus.OK) {
alert(JSON.stringify(status));
}
// This is checking to see if the Geoeode Status is OK before proceeding
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
var address = (results[0].formatted_address);
alert(JSON.stringify(address));
}
});
It doesn't give me the closest address. Note: The data on that gist varies on the latitude/longitude but when I check the other address, only the formatted_address with type neighborhood gives me the closest address from the Lat and Lang.
My Javascript is ropey but I guess this is how I would do it:
function getAddress(results) {
if(results && results.length) {
for (var i=0; i<results.length; i++) {
if (results[i].types.indexOf('neighborhood') != -1) {
return results[i].formatted_address;
}
}
return results[0].formatted_address;
}
return '';
}
I'm using the google geocoder with an option to only return results from Germany
Here's the relevant part of my function
...
var geocoder = new google.maps.Geocoder();
geocoder.geocode({"address":address,"region":"DE" }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0].geometry.location) {
completeGeo(results[0],address);
} else {
completeGeo(null,address);
}
...
but if i geocode "cuvry" too find that street in germany
google returns for example "Cuvry, France" which is outside of the region parameter
How can I prevent google geocoder from returning results that are not in a certain Country?
I mean return, not check in callback if country-code is matching.
This might work using component filters. "components":"country:DE"
var geocoder = new google.maps.Geocoder();
geocoder.geocode({"address":address, "componentRestrictions":{"country":"DE"} },
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0].geometry.location) {
completeGeo(results[0],address);
} else {
completeGeo(null,address);
}
});
When I changed it to
geocoder.geocode({"address":address, "componentRestrictions":{"country":"DE"} },
for germany my search results found vietnam.
I have changed it to this:
geocoder.geocode( {'address':address + ', Deutschland'}, function(results, status)
This works fine with countries name in own language.
The short answer is you can't.
The issue has been filed with Google here:
https://code.google.com/p/gmaps-api-issues/issues/detail?id=4233
The only thing you can try is passing an "accepted bounds" to the geocoder. It's far from perfect but did help a little on my project. Here is a rough copy of the code I use for attempting to limit the Google geocoder to the Western United States. Obviously you'd want to edit the bounds to represent the region you're interested in.
var geocoder = new google.maps.Geocoder()
var callGoogleGeocoder = function(str) {
var geo_bounds;
geo_bounds = setGeoBounds();
return geocoder.geocode({
address: str,
bounds: geo_bounds
}, function(results, status) {
console.log(results);
});
}
var setGeoBounds = function() {
var geo_bounds, ne, sw;
geo_bounds = new google.maps.LatLngBounds();
sw = new google.maps.LatLng(41.24843789608676, -126.5633079709794);
ne = new google.maps.LatLng(49.01224853841337, -108.3479759397294);
geo_bounds.extend(sw);
geo_bounds.extend(ne);
return geo_bounds;
};
Please take a moment to vote on the issue at Google I linked to above. For me, it's the #1 feature the Google Geocoder is missing.
Good luck!
Use the full country name (eg. The Netherlands instead of NL) since the latter did not work (returned results outside NL):
geocoder.geocode({"address":address, "componentRestrictions":{"country":"The Netherlands"} },
I've just got the same problem and solved it this way (for Germany):
var searchObject = {
'address': address,
componentRestrictions: {
country: 'DE'
}
};
geocoder().geocode(searchObject, function (result, status) {
if (status !== google.maps.GeocoderStatus.OK) {
return reject();
}
if (result[0].formatted_address === 'Deutschland' && address !== 'Deutschland') {
return reject();
}
return reject();
});
So, when you search for a wrong address, Google always return Deutschland as address when used componentRestrictions. That's the only way I got it working, because when you remove componentRestrictions Google will result a guessed location, which is not correct for my use case.
Example:
Search for postcode 12345, which is not valid in Germany.
Without componentRestrictions — Result: Schenectady, New York 12345, USA
With Deutschland added to the address — Result: 12345 High Germany Rd SE, Little Orleans, MD 21766, USA
With componentRestrictions — Result: Deutschland
Update
Another solution is to check the partial_match flag:
if (result[0].partial_match) {
return reject();
}
I have tried reverse Geocoding with multiple requests at a time...
My coding is below:
var latlon=new Array();
var lname="";
latlon[0]=new array("11.19","71.20");
latlon[1]=new array("12.89","72.22");
latlon[2]=new array("13.49","73.64");
for(i=0;i<3;i++)
{
lname=locationname(latlon[i][0],latlon[i][1]);
alert(lname);
}
function locationname(lat,lon)
{
var llng = new google.maps.LatLng(lat,lon);
ligeocoder.geocode({'latLng': llng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
if (results[0])
{
loname=results[0].formatted_address;
}
else
{
alert("result failed");
}
}
else
{
alert("Geocoder failed due to: " + status);
}
});
}
It shows error: Geocoder failed due to: OVER_QUERY_LIMIT
This is a limit of the Google geocoding API, not javascript. Basically google will not let you make too many requests at a time, there is no way to get around this limit without breaking the google api terms of service.
If you want to limit the amount of calls you're doing at a time, put your geocoding function in a setInterval.