1

i would like to get a map with 2 markers. if i use the LatLng (see JS Code /WORKS JUST FINE/) it is no problem... but as soon i try to use geocoding (see JS Code /Y U NOT WORKING?/) it doesn't work. I guess this is all wrong:

var adresse3=geocoder.geocode({'address': 'Winkelriedstrasse 47, 6004 Luzern'})[0].geometry.location;

Can anyone help me. Thanks you in advance.

var myCenter=new google.maps.LatLng(47.050944,8.309441);
var geocoder;
var map;
function initialize(){  
    var mapProp = {
      center            :myCenter         
    };
    var geocoder = new google.maps.Geocoder();
    var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
    /*WORKS JUST FINE*/
    var marker1=new google.maps.Marker({
        position: new google.maps.LatLng(47.050944,8.309441)
    });
    marker1.setMap(map);
    var infowindow1 = new google.maps.InfoWindow({
        content:"marker1"
    });
    google.maps.event.addListener(marker1, 'click', function() {
        infowindow1.open(map,marker1);
    });  
    /*Y U NOT WORKING?*/
    var adresse3=geocoder.geocode({'address': 'Winkelriedstrasse 47, 6004 Luzern'})[0].geometry.location;
    var marker3=new google.maps.Marker({
        position: adresse3
    });
    marker3.setMap(map);
    var infowindow3 = new google.maps.InfoWindow({
        content:"marker3"
    });
    google.maps.event.addListener(marker3, 'click', function() {
        infowindow3.open(map,marker3);
    }); 
}
google.maps.event.addDomListener(window, 'load', initialize);
1
  • What error do you get? Commented Apr 12, 2013 at 14:51

3 Answers 3

1

Your problem is that geocoder.geocode is asynchronous, so you have to pass a callback function.

Adapted from the Google Maps Documentation :

geocoder.geocode({'address': 'Winkelriedstrasse 47, 6004 Luzern'}, function(results, status)     {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
    });
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

thats how i solved it (i used the goMap PlugIn http://www.pittss.lv/jquery/gomap/ )

<script type="text/javascript" src="js/jquery.gomap-1.3.2.min.js"></script> 
<script type="text/javascript">
$(document).ready(function() {
    $(function(){ 
        $("#googleMap").goMap({ 
            address: 'Luzern, Schweiz', 
            zoom: 13,
            maptype: 'ROADMAP',
            mapTypeControl: false,  
            hideByClick: false
        }); 
        $.goMap.createMarker({  
            address: 'Winkelriedstrasse 47, 6004 Luzern',
            html: 'Winkelriedstrasse 47, 6004 Luzern' 
        });
    });
});
</script>

now it works just fine...

Comments

0

Nicolas, you have an error in line:

geocoder.geocode('address': 'Winkelriedstrasse 47, 6004 Luzern', function(results, status) 

use this code:

geocoder.geocode({'address': 'Winkelriedstrasse 47, 6004 Luzern'}, function(results, status) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.