admin管理员组文章数量:1379856
I want the infoWindow to open on the specific marker every time I click on the marker. But every time it stays in the same position where it used to be at the last time. Here's my code
var infowindow = new google.maps.InfoWindow(
{
size: new google.maps.Size(150,20)
});
var markersArray = [];
var countMarker = 0;
function initialize() {
geocoder = new google.maps.Geocoder();
var myLocation = new google.maps.LatLng(52.13206069538749, -106.63635849952698);
var mapOptions = {
zoom: 12,
center: myLocation,
mapTypeControl: true,
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("mapCanvas"), mapOptions);
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng);
infowindow.setPosition(marker.getPosition());
infowindow.setContent(event.latLng.toString());
infowindow.open(map,marker);
// showing latitude and longitude in infowindow
if (flag == 1){
document.getElementById("latbox").value=event.latLng.lat();
document.getElementById("lngbox").value=event.latLng.lng();
flag++;
}
else{
var current_lat = event.latLng.lat().toString();
var current_lng = event.latLng.lng().toString();
insRow(current_lat, current_lng, marker.id);
}
google.maps.event.addListener(marker, 'click', function() {
// addMarker(event.latLng);
// infowindow.setPosition(event.latLng);
infowindow.setPosition(marker.getPosition());
infowindow.setContent(event.latLng.toString());
//infowindow.open(map,event.latLng);
});
});
//document.write("3");
google.maps.event.trigger(marker, 'click');
}
function addMarker(location) {
countMarker++;
marker = new google.maps.Marker({
position: location,
map: map,
id: countMarker
});
markersArray.push(marker);
//alert(marker.id);
}
Can anyone help me in this regard please?
I want the infoWindow to open on the specific marker every time I click on the marker. But every time it stays in the same position where it used to be at the last time. Here's my code
var infowindow = new google.maps.InfoWindow(
{
size: new google.maps.Size(150,20)
});
var markersArray = [];
var countMarker = 0;
function initialize() {
geocoder = new google.maps.Geocoder();
var myLocation = new google.maps.LatLng(52.13206069538749, -106.63635849952698);
var mapOptions = {
zoom: 12,
center: myLocation,
mapTypeControl: true,
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("mapCanvas"), mapOptions);
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng);
infowindow.setPosition(marker.getPosition());
infowindow.setContent(event.latLng.toString());
infowindow.open(map,marker);
// showing latitude and longitude in infowindow
if (flag == 1){
document.getElementById("latbox").value=event.latLng.lat();
document.getElementById("lngbox").value=event.latLng.lng();
flag++;
}
else{
var current_lat = event.latLng.lat().toString();
var current_lng = event.latLng.lng().toString();
insRow(current_lat, current_lng, marker.id);
}
google.maps.event.addListener(marker, 'click', function() {
// addMarker(event.latLng);
// infowindow.setPosition(event.latLng);
infowindow.setPosition(marker.getPosition());
infowindow.setContent(event.latLng.toString());
//infowindow.open(map,event.latLng);
});
});
//document.write("3");
google.maps.event.trigger(marker, 'click');
}
function addMarker(location) {
countMarker++;
marker = new google.maps.Marker({
position: location,
map: map,
id: countMarker
});
markersArray.push(marker);
//alert(marker.id);
}
Can anyone help me in this regard please?
Share Improve this question edited Jul 7, 2011 at 21:05 Khepri 9,6275 gold badges48 silver badges61 bronze badges asked Jul 7, 2011 at 21:04 PowPow 1,36710 gold badges32 silver badges57 bronze badges1 Answer
Reset to default 4try this:
google.maps.event.addListener(marker, "click", function (event) {
infoWindow.setContent(this.position);
infoWindow.open(map, this);
});
Also, I do not see in the code where your are defining the variable marker.
You might want to add change addMarker(event.latLng);
to
var marker = addMarker(event.latLng);
and change the function to return the marker object:
function addMarker(location) {
countMarker++;
var marker = new google.maps.Marker({
position: location,
map: map,
id: countMarker
});
markersArray.push(marker);
return marker;
}
本文标签: javascriptGoogle Maps API v3 InfoWindow is not in right positionStack Overflow
版权声明:本文标题:javascript - Google Maps API v3: InfoWindow is not in right position - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744440657a2606412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论