admin管理员组文章数量:1356378
I'm trying to open the info window when a user clicks on a link from outside the map. Not sure what I'm missing here. Basically, I have a list of cities with an id for each (var ids loops over the ids). When the user clicks on that link (on click="myclick('1')" id like the infowindow for that city(1 = london etc) to open up.
Any help would be appreciated.
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() {};
I'm trying to open the info window when a user clicks on a link from outside the map. Not sure what I'm missing here. Basically, I have a list of cities with an id for each (var ids loops over the ids). When the user clicks on that link (on click="myclick('1')" id like the infowindow for that city(1 = london etc) to open up.
Any help would be appreciated.
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() {};
Share
Improve this question
edited Jul 8, 2019 at 19:55
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Jan 31, 2012 at 11:07
GhatziGhatzi
5972 gold badges12 silver badges24 bronze badges
1 Answer
Reset to default 9It looks as if myclick will not be available in global context right now.
Try to explicitly assign it to the context(window in this case):
this.myclick=function(i) {
google.maps.event.trigger(gmarkers[i], 'click');
};
本文标签: javascriptGoogle maps v3 open infowindow from a link outside of mapStack Overflow
版权声明:本文标题:javascript - Google maps v3 open infowindow from a link outside of map - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743978142a2570961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论