geocoder.geocode returns empty string for status.

565 views
Skip to first unread message

ik6s

unread,
Aug 4, 2011, 1:42:01 PM8/4/11
to google-maps-ap...@googlegroups.com

I have the following code ( I have also attached a aspx file) which is very simple and has a map which initializes to the users location, and the user can choose a location from the dropdown to show on the map. The initialization works fine, but when I debug the code in firebug, I can see that geocoder.geocode is returning empty string for status. Does the call to api fail, or I am not sure what is happening. Please help. I have spent two days on this. Thanks.


<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
//center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
// Try W3C Geolocation (Preferred)
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
map.setCenter(initialLocation);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
// Try Google Gears Geolocation
} else if (google.gears) {
browserSupportFlag = true;
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
map.setCenter(initialLocation);
}, function() {
handleNoGeoLocation(browserSupportFlag);
});
// Browser doesn't support Geolocation
} else {
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
function handleNoGeolocation(errorFlag) {
if (errorFlag == true) {
alert("Geolocation service failed.");
initialLocation = newyork;
} else {
alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
initialLocation = siberia;
}
map.setCenter(initialLocation);
}
}
var map;
var geocoder;
function codeAddress(sAddress)
{
geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': sAddress},function(results, status){});
}
function showAddress(sAddress){
codeAddress(sAddress);
if ( status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);
}
}
function findAddress() {
if (document.getElementById("ddlCompanies") == null )
return false;
else{
var ddlAddress = document.getElementById("ddlCompanies");
var ddlAddressIndex = ddlAddress.selectedIndex;
var ddlAddressValue = ddlAddressIndex.value;
var address = ddlAddress.options[ddlAddress.selectedIndex].value;
showAddress(address);
return false;
}
}
Default.aspx

Chris Broadfoot

unread,
Aug 10, 2011, 2:05:49 AM8/10/11
to google-maps-ap...@googlegroups.com
On Fri, Aug 5, 2011 at 3:42 AM, ik6s <iden....@gmail.com> wrote:
geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': sAddress},function(results, status){});
}

Your callback doesn't do anything. The V3 Geocoder is asynchronous, so status will only be available within the callback function.

In future, best to try V3 group for JS-related questions: http://groups.google.com/group/google-maps-js-api-v3

Chris

--


Reply all
Reply to author
Forward
0 new messages