2013-01-10 24 views
0

我從這個例子的工作事件偵聽器:http://storelocator.googlecode.com/git/examples/panel.html谷歌地圖 - 由商店位置創建標記

我希望能夠添加一個偵聽所有類似的標記,但我不知道如何引用它們,因爲它們是由商店定位器從DataFeed創建的。

google.maps.event.addListener(marker, 'click', //myCode); 

這是從例如確切面板代碼:

google.maps.event.addDomListener(window, 'load', function() { 
    var map = new google.maps.Map(document.getElementById('map-canvas'), { 
    center: new google.maps.LatLng(-28, 135), 
    zoom: 4, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    var panelDiv = document.getElementById('panel'); 

    var data = new MedicareDataSource; 

    var view = new storeLocator.View(map, data, { 
    geolocation: false, 
    features: data.getFeatures() 
    }); 

    new storeLocator.Panel(panelDiv, { 
    view: view 
    }); 
}); 
+0

這不是最好的例子修改。請看Google自己的[DEMO](https://developers.google.com/maps/articles/phpsqlsearch_v3)。雖然這使用了數據庫,但可以修改它以使用其他數據源 –

回答

1

this example代替。如果您只想定製infowindow內容,只需使用該部分即可。

// Store locator with customisations 
// - custom marker 
// - custom info window (using Info Bubble) 
// - custom info window content (+ store hours) 

var ICON = new google.maps.MarkerImage('medicare.png', null, null, 
    new google.maps.Point(14, 13)); 

var SHADOW = new google.maps.MarkerImage('medicare-shadow.png', null, null, 
    new google.maps.Point(14, 13)); 

google.maps.event.addDomListener(window, 'load', function() { 
    var map = new google.maps.Map(document.getElementById('map-canvas'), { 
    center: new google.maps.LatLng(-28, 135), 
    zoom: 4, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    var panelDiv = document.getElementById('panel'); 

    var data = new MedicareDataSource; 

    var view = new storeLocator.View(map, data, { 
    geolocation: false, 
    features: data.getFeatures() 
    }); 

    view.createMarker = function(store) { 
    var markerOptions = { 
     position: store.getLocation(), 
     icon: ICON, 
     shadow: SHADOW, 
     title: store.getDetails().title 
    }; 
    return new google.maps.Marker(markerOptions); 
    } 

    var infoBubble = new InfoBubble; 
    view.getInfoWindow = function(store) { 
    if (!store) { 
     return infoBubble; 
    } 

    var details = store.getDetails(); 

    var html = ['<div class="store"><div class="title">', details.title, 
     '</div><div class="address">', details.address, '</div>', 
     '<div class="hours misc">', details.hours, '</div></div>'].join(''); 

    infoBubble.setContent($(html)[0]); 
    return infoBubble; 
    }; 

    new storeLocator.Panel(panelDiv, { 
    view: view 
    }); 
}); 

甚至做這樣的事情:

view.createMarker = function(store) { 
    var markerOptions = { 
    position: store.getLocation(), 
    title: store.getDetails().title 
    }; 
    var marker = new google.maps.Marker(markerOptions); 
    google.maps.event.addListener(marker, "click", function() { 
     alert("my code"); 
    }); 
    return marker; 
} 

working example