2

Is it possible to do that?

This is what I have found in Google https://groups.google.com/forum/?fromgroups=#!topic/google-maps-js-api-v3/kdgueJzwvK4.

I have made this:

  # TODO In case input has currently a value
  if autocomplete.getPlace()
    places.pin(autocomplete.getPlace(), map, input)

But autocomplete.getPlace() returns undefined when user has not triggered some event.

After user enters some data I can use places.pin(autocomplete.getPlace(), map, input) without problem

I tried to trigger change in the input, but had no luck $(input).trigger('change')

sites
  • 21,417
  • 17
  • 87
  • 146
  • Not sure if this is what you are looking for but in the past I have been able to force the initial location of map using `// Use Geocoder to geolocate geocoder = new google.maps.Geocoder(); // Set default myLatLng to be used if google.loader.ClientLocation fails var myLatlng = new google.maps.LatLng(40.8802950337396, -102.21679674999996); // If ClientLocation was filled in by the loader, use that info instead if (google.loader.ClientLocation) { gmapZoom = 13; myLatlng = new google.maps.LatLng( google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude); }` – Dropzilla May 01 '13 at 16:56
  • I forgot to tell that I have found this answer http://stackoverflow.com/questions/10772282/google-maps-places-api-v3-autocomplete-select-first-option-on-enter-and-have, where there is a fiddle that sets location according long lat, but I need to set map according what is in input. Seems your solutions is also for long lat. In that case how could I extend your solution to meet my needs? – sites May 01 '13 at 17:19
  • Seems the answer is there, but there is too much code :( – sites May 01 '13 at 17:22

1 Answers1

3

Here is some code I wrote a long time ago that may point you in the right direction. There may be some gaps you need to fill in.

<!DOCTYPE html>
     <html lang="en">
          <head>
          </head>
     <body>
         <input id="gmap_start_address" type="text" />
     </body>
</html>

initialize();
        // Handle enter button click event for start address input text field
        $("#gmap_start_address").keypress(function(event){
            if(event.keyCode == 13){
                event.preventDefault();
                goStartAddr();
            }
        });

/**
 * Handle initialization of google map
 * @params
 */
function initialize() {
    var gmapZoom = 4;
    geocoder = new google.maps.Geocoder();
    // Set default myLatLng to be used if google.loader.ClientLocation fails
    var myLatlng = new google.maps.LatLng(40.8802950337396, -102.21679674999996);
    // If ClientLocation was filled in by the loader, use that info instead
    if (google.loader.ClientLocation) {
        gmapZoom = 13;
        myLatlng = new google.maps.LatLng( google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
    }
    var myOptions = {
        zoom: gmapZoom,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var polyOptions = {
        strokeColor: '#5F5CFA',
        strokeOpacity: 0.5,
        strokeWeight: 3
    }
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);
    // Add a listener for the click event
    google.maps.event.addListener(map, 'click', addLatLng);
}

/**
 * Handle GO button click to process start address
 * @params
 */
function goStartAddr(){
    var startAddr = $('#gmap_start_address').val();
    if(startAddr != '' && startAddr != 'Starting address, city, zip code'){
        placeStartMkr();
    } else {
        alert('Please enter start address.');
        $('#gmap_start_address').focus();
    }
}
/**
 * Handle marker placement from human address start point.
 * Originally this function placed a starting marker
 * Starting marker code is commented out and preserved for
 * ease of use in the future.
 * @params
 */
function placeStartMkr(){
    // Grab address
    var humanAddr = $('#gmap_start_address').val();
    geocoder.geocode( { 'address': humanAddr}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            map.setZoom(16);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
Dropzilla
  • 502
  • 3
  • 14