have map; can't update map center and zoom when user inputs street address

35 views
Skip to first unread message

Michael Laird

unread,
Jan 17, 2012, 4:17:57 PM1/17/12
to google-map...@googlegroups.com
I'm new at this, but I have a Google V3 map that loads from the user's W3C browser geocode info, and I pull country, state, and city from the geocode object and update input boxes. If the user enters a street address, I want to geocode it via Google and change map.setCenter and setZoom, and display the updated map. When that works, I want to add a marker and infowindow. Despite hours of research and trials, I can't get the geocode and update to work. The Developer Tool in Chrome seems to indicate that execution ceases/fails at the geocoder.geocode line that is indicated in bold below. This is a localhost development, but here's relevant code.

var map, geocoder, marker, infowindow; //global variables

function initializeMap() {
// Try W3C Geolocation to geolocate desktop user
//initialize Google map, geolocate desktop user, and display after page loads
//find country, state, and city for the user's location  -  this all works
}
window.onload = initializeMap;

//change the Google Map after a user enters a street address
$("#streetAddress").blur(function() {
    //if state, city and street address locations are present
if ( $(this).val().length > 0 && $("#state").val().length > 0 && $("#city3").val().length > 0  ) {
    var gAddress =  [$(this).val() + ", " + $("#city3").val() + ", " + $("#state").val()] ;
    //get coordinates of this address
    //if no geocode object exists, create one
    if (!geocoder) {
    geocoder = new google.maps.Geocoder(); //create Geocoder object else use existing one from initializeMap() ?
    }       
    //create a GeocoderRequest object with user's street address
    var geoCoderRequest = {address:  gAddress} 
    //make a Geocoder request
    geocoder.geocode( geoCoderRequest, function(results, status) {  //this line fails
    //check if status is OK
            if ( status === google.maps.GeocoderStatus.OK) {
            //update center of existing map on location returned for this address
            map.setCenter(results[0].geometry.location);
            map.setZoom(14);
            }   
    });         
    } else {
            return; //no action if gAddress is incomplete
        }
});

JKurtock

unread,
Jan 17, 2012, 7:59:51 PM1/17/12
to Google Maps JavaScript API v3


On Jan 17, 1:17 pm, Michael Laird <mla...@alum.mit.edu> wrote:
...
> This is a localhost development, but here's relevant code.

Really? How do you know?

In my personal experience, if I knew what code was the relevant code,
I could find the problem.

Put your code on a free server somewhere, so folks on this site will
have a chance of duplicating your problem, and have the browser
debugging tools available to find it.

And don't feel bad if in the process of creating an accessible site,
you find the problem. Happens all the time.

- Jeff
Reply all
Reply to author
Forward
0 new messages