3

当用户点击地图之外的链接时,我试图打开信息窗口。不知道我在这里错过了什么。基本上,我有一个城市名单,每个城市都有一个ID(var id ids遍布整个ID)。当用户点击该链接(在点击=“MyClick认证(‘1’)” ID,如信息窗口那城(1 =伦敦等)开拓。谷歌地图v3从地图之外的链接打开infowindow

任何帮助,将不胜感激。

var url = "my json link"; 
var gmarkers = []; 

function initialize() { 

var myLatlng = new google.maps.LatLng(0, 0); 
var myOptions = { 
    zoomControl: true, 
    disableDefaultUI: true, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

downloadUrl(url, function(data) { 

var j = eval('(' + data.responseText + ')'); 
var jlength = j.data.cities.length; 

var bounds = new google.maps.LatLngBounds(); 

for(i=0; i < jlength; i++) { 

    var x = parseFloat(j.data.cities[i].lat); 
    var y = parseFloat(j.data.cities[i].lon); 
    var ids = parseFloat(j.data.cities[i].id); 
    var z = new google.maps.LatLng(x,y); 
    var title = j.data.cities[i].title; 
    var contentstring = 'text' 

    var marker = createMarker(ids); 

    var infowindow = new google.maps.InfoWindow({content: contentstring}); 

    bounds.extend(z); 
    map.fitBounds(bounds); 

}; 

if (map.getZoom() == 21) 
    { 
     map.setZoom(16); 
    } 

if (map.getZoom() < 5) 
    { 
     map.setZoom(map.getZoom()+1); 
    } 


    function myclick(i) { 
     google.maps.event.trigger(gmarkers[i], 'click'); 
    }; 


    function createMarker(){ 

     var marker = new google.maps.Marker({ 
      position: z, 
      map: map, 
      title: title, 
      html: contentstring, 
      icon: 'imagelink' 
     }); 

     google.maps.event.addListener(marker,'click',function(){ 
     infowindow.setContent(this.html); 
     infowindow.open(map,marker); 
     }); 

      //google.maps.event.addListener(marker,'click',function(){ 
      //window.location.href = marker.url; 
      //}); 

     gmarkers[ids] = marker; 

    }; 

}); 

}; 

google.maps.event.addDomListener(window, 'load', initialize); 

function downloadUrl(url, callback) { 
    var request = window.ActiveXObject ? 
     new ActiveXObject('Microsoft.XMLHTTP') : 
     new XMLHttpRequest; 

    request.onreadystatechange = function() { 
    if (request.readyState == 4) { 
     request.onreadystatechange = doNothing; 
     callback(request, request.status); 
    } 
    }; 

    request.open('GET', url, true); 
    request.send(null); 
} 

function doNothing() {}; 

回答

9

看起来好像了MyClick不会是现在全球范围内提供

尽量明确它(在这种情况下,窗口)分配到上下文:

this.myclick=function(i) { 
     google.maps.event.trigger(gmarkers[i], 'click'); 
    }; 
+0

感谢反应,但仍然没有喜欢害怕。 – Hatzi 2012-01-31 12:36:26

+0

然后你犯了一个错误,它适用于我:http://jsfiddle.net/sRTkp/ – 2012-01-31 15:06:23

+1

你的明星,虽然它还没有完全解决我的问题,但我现在知道问题在于我打电话地图和来自不同xslt的链接。你上面的代码是我的问题的答案。非常感谢。 – Hatzi 2012-01-31 15:21:29