1

我添加了多個標記,我想要做的是點擊,獲得特定的標記位置。谷歌地圖得到一個標記的位置

但是目前它確實有效,但它顯示最後一個標記的lat和lng。如何解決這個問題,當我點擊標記時,它會給我那個標記的特定位置。

function algolia_search(position) { 
    clearOverlays(); 
var APPLICATION_ID = '75RQSC1OHE'; 
var SEARCH_ONLY_API_KEY = 'f2f1e9bba4d7390fc61523a04685cf12'; 
var INDEX_NAME = 'businesses'; 
var PARAMS = { hitsPerPage: 20 }; 
// Client + Helper initialization 
var algolia = algoliasearch(APPLICATION_ID, SEARCH_ONLY_API_KEY); 
var algoliaHelper = algoliasearchHelper(algolia, INDEX_NAME, PARAMS); 
// Map initialization 
algoliaHelper.on('result', function(content) { 
    renderHits(content); 
    var i; 
    // Add the markers to the map 
    for (i = 0; i < content.hits.length; ++i) { 
    var hit = content.hits[i]; 
    var marker = new google.maps.Marker({ 
     position: {lat: hit._geoloc.lat, lng: hit._geoloc.lng}, 
     map: map, 
     label: hit._geoloc.slug, 
     animation: google.maps.Animation.DROP 
    }); 
    markers.push(marker); 
    marker.addListener('click', function() { 
     var destinationLat = marker.getPosition().lat(); 
     var destinationLng = marker.getPosition().lng(); 
     console.log(lat); 
     console.log(lng); 
     console.log(destinationLat); 
     console.log(destinationLng); 

回答

1

你需要在地圖上的一個listerner的點擊在event.latLng你有座標

google.maps.event.addListener(map, 'click', function(event) { 
    alert ('Lat : ' + event.latLng.lat() + ' Lng : ' + event.latLng.lng()) 
}); 

你已經放置標記在地圖上的事實,你可以使用一個封閉

var addListenerOnPoint = function(actMark){ 

    actMark.addListener('click', function() { 
     alert ('Lat : ' + actMark.position.lat() + ' Lng : ' +actMark.position.lng()); 
    }); 


    for (i = 0; i < content.hits.length; ++i) { 
     var hit = content.hits[i]; 
     var marker = new google.maps.Marker({ 
     position: {lat: hit._geoloc.lat, lng: hit._geoloc.lng}, 
     map: map, 
     label: hit._geoloc.slug, 
     animation: google.maps.Animation.DROP 
    }); 
    addListenerOnPoint(marker, 
       ); 
    markers.push(marker); 

    } 
+0

不起作用仍然帶給我相​​同的經度和緯度 –

+0

..不可能...這個listerner返回點你在地圖上點的座標..可能是,你已經把代碼放在錯誤的地方.. – scaisEdge

+0

好吧,當我點擊地圖時,但不是當我點擊標記 –