Google maps api v3 geocoder viewport - google-maps

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().

Related

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 Autocomplete API X Geocoder

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);
}
});

how to add or show zip code boundary Google maps v3

After searching zip code through geocoder i want to show boundary around that zip code region as shown in google maps. i have search 02201 as example in maps.google.com and it shows boundary around the whole zip region which indicates all the area under that zip range. here is my code which search zipcode using geocoder and i want to add boundary around that zip region.that i want to implement. please provide any solution. please see this link for image http://i46.tinypic.com/beyerq.jpg
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);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
Sounds like you are trying to do something like this, but with a query to show only the specified zip code polygon. Like this similar post or this one.

Google Maps v3, reverse Geocoding and multiple Infowindow

I'm working on a google map v3.
On this map I want display several markers each one with its Infowindow where shows some information about that point.
The source of information is an javascript array with some data for each point, and up here everything works fine.
The array contains (SOMETIMES) the address (sometimes null) and ALWAYS lat-long coordinates, so when the address isn't present I have to make a reverse-geocoding. Here my code:
var geocoder = new google.maps.Geocoder();
for(var i=0;i<markersArray.length;i++){
var la=markersArray[i][0]);
var lo=markersArray[i][1]);
gpoint=new google.maps.LatLng( la,lo);
var aMarker = new MarkerWithLabel({ //part of Google Maps Utility Lib
position: gpoint,
map: map,
labelContent: deviceID,
labelAnchor: new google.maps.Point(22, 0),
labelClass: "labelStyle",
html: "<ul><li>Speed: "+markersArray[i][3]+"</li></ul>",
address: markersArray[i][4]
});
if (aMarker.address<="" || aMarker.address==null) {
geocoder.geocode({'latLng': gpoint}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
aMarker.address=results[1].formatted_address;
}
}
});
}
google.maps.event.addListener(aMarker, 'click', function () {
infowindow.setContent(this.html+" "+this.address);
infowindow.open(map, this);
});
....
}
Almost everything is ok: the markers are in position and all them show the right infowindow with the right addresses except those where the addresses were empty and were reverse-geocoding.
For this last set only the last one marker infowindow shows the right address, all other infowindow addresses's are empty.
Any idea?
Very thanks!
One way to fix this is to create a function which does the reverse geocoding and can have function closure on the infowindow and the marker.
As the reverse geocode operation is asynchronous, the returned address isn't available for any except the last marker.