2014-03-31 40 views
1

我遇到了Google Maps API v3的問題,並讓我的監聽器提供了返回結果。谷歌地圖標記監聽器問題

我已經添加了的jsfiddle這裏http://jsfiddle.net/f2DqW/4/

誰能告訴我什麼,我做錯了什麼?

當你點擊一個標記時,我希望它從地址(這將很快得到更多的信息)和我的郵編在屏幕上回顯。最終我想要一個彈出窗口,但現在這並不重要。

我的代碼是:

var geocoder; 
var map; 
function codeAddress() { 

var address = 'SW3, United Kingdom'; 
geocoder.geocode({ 'address': address}, 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); 
    } 
}); 
} 

function initialize() { 


$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=SW3, United Kingdom', null, function (data) { 
      var p = data.results[0].geometry.location 
      var latlng = new google.maps.LatLng(p.lat, p.lng); 
      $('#latId').val(p.lat); 
      $('#latId2').val(p.lng); 
     }); 

var lat = $('#latId').val(); 
var lng = $('#latId2').val(); 


var map; 
    var elevator; 
    var myOptions = { 
     zoom: 10, 
     center: new google.maps.LatLng(lat,lng), 
     mapTypeId: 'terrain' 
    }; 

    map = new google.maps.Map($('#map_canvas')[0], myOptions); 

    var addresses = ['SW3', 'SW2', 'SW1']; 

    for (var x = 0; x < addresses.length; x++) { 


     $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[x]+'&sensor=false', null, function (data) { 
      var p = data.results[0].geometry.location 
      var latlng = new google.maps.LatLng(p.lat, p.lng); 
      var marker = new google.maps.Marker({ 
       position: latlng, 
       map: map, 
       title: 'test' 
      }); 



google.maps.event.addListener(marker, 'click', function() { 
    alert(addresses[x]); 
     map.setCenter(marker.getPosition()); 

    }); 

    marker.setMap(map); 



     }); 
    } 
} 

提前非常感謝。

+0

小提琴真的會解決這個問題 – gimg1

+0

http://jsfiddle.net/f2DqW幫助/ 4 / –

回答

0

這是一個經典之處:變量x不包含您點擊標記時期待的值。它保存地址。長度。我會做這樣的事情:

 $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[x]+'&sensor=false', null, (function(address) { return function (data) { 
      var p = data.results[0].geometry.location 
      var latlng = new google.maps.LatLng(p.lat, p.lng); 
      var marker = new google.maps.Marker({ 
       position: latlng, 
       map: map, 
       title: 'test' 
      }); 



google.maps.event.addListener(marker, 'click', function() { 
    alert(address); 
     map.setCenter(marker.getPosition()); 

    }); 

    marker.setMap(map); 



     };})(addresses[x])); 
0
 for (var x = 0; x < addresses.length; x++) { 

     $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=' + addresses[x] + '&sensor=false', null, function (data) { 

      var p = data.results[0].geometry.location 
      var latlng = new google.maps.LatLng(p.lat, p.lng); 

      var marker = new google.maps.Marker({ 
       position: latlng, 
       map: map, 
       title: 'test' 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       geocoder.geocode({ 
        'latLng': latlng 
       }, function (results, status) { 
        if (status == google.maps.GeocoderStatus.OK) { 
         $.each(results[0].address_components, function (i, item) { 
          if (item.types[0] == 'postal_code') { 
           alert(item.long_name); 
          } 
         }); 
        } 
       }); 
       map.setCenter(marker.getPosition()); 
      }); 

      marker.setMap(map); 
     }); 
    } 
}