使用 JQuery 输出中的数据数组在 Google 地图版本 3 上创建自定义信息窗口
由于 Google 地图版本 3 的 API 仍在不断发展,我不希望使用任何插件。我的版本 2 的原始脚本可以完美地显示自定义信息窗口,该窗口在单击标记时出现,在单击地图区域时消失。新的 API 引入了一个对象 google.maps.OverlayView,我很难将其与 JQuery 结合使用。我的原始代码示例如下所示:
function Results(json) {
var L = json.output.length;
var website = json.web;
if (L > 0) {
for(i=0; i<L; i++) {
var listing = json.output[i];
addLocation(listing, website);
}
}
}
function addLocation(A, B) {
var point = new GLatLng(A.lat, A.lng);
var icon = new GIcon(baseIcon);
icon.image = 'images/' + A.mks + '.png';
var M = new GMarker(point, icon);
map.addOverlay(M);
GEvent.addListener(M, 'click', function(){
showInfo(M, '<A href=\"site.php?ID=' + A.id + '\">' + A.name + '</A>');
});
GEvent.addListener(map, 'click', function(){
hideInfo();
});
showResult(A, B);
}
function showInfo(M, text){
var markerOffset = map.fromLatLngToDivPixel(M.getPoint());
$('#info').appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));
$('#info').show()
.css({ top:markerOffset.y, left:markerOffset.x })
.html(text);
}
function hideInfo(){
$('#info').hide();
}
var Q = 'search.php'; // an array is output in Json format
$.getJSON(Q, Results);
我能找到的最接近的示例是: http://www.tdmarketing.co.nz/blog/2011/03/09/create-marker-with-custom-labels-in-google-maps-api-v3/这离我的想法还很远。
我不确定是否有人有想法改造上面的内容,以便它可以与 API 版本 3 一起使用。
As the API for Google Maps version 3 is still evolving, I do not wish to use any plugins. My original script for version 2 works perfectly to display custom infowindow that appears when a marker is clicked and disappears when the map region is clicked. The new API introduces an object google.maps.OverlayView, which I have difficulty using in conjunction with JQuery. A sample of my original code is shown below:
function Results(json) {
var L = json.output.length;
var website = json.web;
if (L > 0) {
for(i=0; i<L; i++) {
var listing = json.output[i];
addLocation(listing, website);
}
}
}
function addLocation(A, B) {
var point = new GLatLng(A.lat, A.lng);
var icon = new GIcon(baseIcon);
icon.image = 'images/' + A.mks + '.png';
var M = new GMarker(point, icon);
map.addOverlay(M);
GEvent.addListener(M, 'click', function(){
showInfo(M, '<A href=\"site.php?ID=' + A.id + '\">' + A.name + '</A>');
});
GEvent.addListener(map, 'click', function(){
hideInfo();
});
showResult(A, B);
}
function showInfo(M, text){
var markerOffset = map.fromLatLngToDivPixel(M.getPoint());
$('#info').appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));
$('#info').show()
.css({ top:markerOffset.y, left:markerOffset.x })
.html(text);
}
function hideInfo(){
$('#info').hide();
}
var Q = 'search.php'; // an array is output in Json format
$.getJSON(Q, Results);
The closest example I can find is: http://www.tdmarketing.co.nz/blog/2011/03/09/create-marker-with-custom-labels-in-google-maps-api-v3/ which is still quite far off from what I have in mind.
I am not sure if anyone has an idea of transforming the above so that it can work with API version 3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显示结果(数据数组);让它发挥作用。我现在只是将一些示例数据放入 JSON 位中。
displayResults( data_array ); to get it working. I just put some sample data in the JSON bit for now.