Google Maps Autocomplete API X Geocoder - google-maps

I'm trying to use Google Maps Autocomplete API but there is some discrepancies between it and Geocoder API, for example:
On the autocomplete API, I'm trying to search the address "Rua Tiradentes, 1020, Ibirubá", when I select on the list, the address component "street_number" doesn't come, it's an issue with this and other addresses.
Let's try with geocoder API:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': 'Rua Tiradentes, 1020, Ibirubá' }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
} else {
alert("Geocoder failed due to: " + status);
}
});
Now, the street_number is inside the address_components, is there any workaround or Google Autocomplete isn't reliable?
Here is the places code:
var b = new google.maps.places.Autocomplete(myInput);
google.maps.event.addListener(b, 'place_changed', function () {
console.log(b.getPlace());
})

The possible workaround is the following. You can try to geocode the place to get all address components. Recently, Google added possibility to geocode addresses using the placeId as a request parameter.
You can find more detailed information in the documentation:
https://developers.google.com/maps/documentation/javascript/geocoding
https://developers.google.com/maps/documentation/javascript/3.exp/reference#GeocoderRequest
So, you can get the place like var place = autocomplete.getPlace(); you can obtain the place ID like place.place_id and execute geocoding request like this one:
geocoder.geocode( { 'placeId': place.place_id}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});

Related

Best way to geocode Ukraine postcode with Google Maps API?

What is the most effective and accurate way to geocode a Ukraine postcode? Using the built in geocode object results in massively blurred results (lots of postcodes returning just a general area).
Are there any free Ukraine postcode geocoding services I can plug into via JavaScript?
If I´ve understand your Question correct, you could simply set a componentRestrictions. Could look like this:
doGeocode: function (address, postal_code, callback) {
console.log("TEST: " + address.toString());
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address,
'componentRestrictions': {
'postalCode': postal_code,
'country': 'de' //change this to the Code of Ukraine
}
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
console.log(results);
callback(results);
} else {
//Error handling
alert('Geocode was not successful for the following reason: ' + status);
}
});
Note: Of course you have to set the country code for Ukraine here. And, because you didn´t mentioned which information you have either address or lat/lng, I just gave you an example for addresses.

google maps api can't find one specific address

I am using google maps api and it works for all but one specific address. It's "99p Stores 19-20 Market Place Wisbech PE13 1DZ". When using google maps I can find it, but using js api it says 'zero results'.
My code:
function mapsSetMark(map, address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
map.setZoom(13);
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
console.log('Geocode was not successful for the following reason: ' + status);
}
});
}
"99p Stores 19-20 Market Place Wisbech PE13 1DZ" is a place not a postal address. The Geocoder is specifically for postal addresses
It can find "19-20 Market Place Wisbech PE13 1DZ"
To find "99p Stores 19-20 Market Place Wisbech PE13 1DZ" use the Places API Library

How to get the name of the region where the marker is placed in Google Maps API, gmaps.js?

i should get name of region by coordinates using Google Maps Api or gmaps.js
If you know how do it, please give me answer. And sorry for my english.
You need to use the Geocoder service to get location details from coordinates. The response from the Geocoder will contain all the details. All you need is to find out the details you are interested in.
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'latLng': latLng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
}
});
See this also http://jsfiddle.net/eB2RX/1/

Google maps api v3 geocoder viewport

when I enter a location (city or country) and an infowindow is open the following code will only find locations within the area of the open infowindow:
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.fitBounds(results[0].geometry.viewport);
} else {
alert("Sorry, we couldn't find the location for the following reason: " + status);
}
});
}
I want to zoom to the location viewport WITHOUT the fitBounds
This kind of works:
map.setZoom(13);
But I don't want to specify the zoom level
Here's a link from the latter code: http://www.hostelbars.com/map_test_v3_3.html
I'd appreciate some help - thanks!
Perhaps you want to constrain the geocoding to the bounds of the visible map, rather than the open infowindow?
You need to tell the geocoder about your desired bounds in the request not when handling the result.
Refer to http://code.google.com/apis/maps/documentation/javascript/reference.html#GeocoderRequest
Set the bounds in the geocoderRequest to map.getBounds().

How to geocode in google maps v3

I've been looking through version 3 of the google maps api, and I have put together a small script to geocode an address. The problem is that I want to see If i can extract the lat lng without having to 'split()' the result.
function getLatLng() {
var geocoder = new google.maps.Geocoder();
var query = "12 henry street, dublin 1, dublin, ireland";
var address = query;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;
alert('Geocode succesful ' + latLng);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
I want to get the lat and the lng from the var latLng.
Can this be done without calling latLng.split(',');?
Thanks so much for your help
Never use internal variables exposed by the API. They can and do change from release to release. Always use documented methods.
results[0].geometry.location is a LatLng object, so use the relevant methods:
var lat=results[0].geometry.location.lat();
var lng=results[0].geometry.location.lng();
You can use this:
var lat= results[0].geometry.location.lat();
var lng= results[0].geometry.location.lng();
alert(lat);
alert(lng);