我有一个叠加图,这是代码(代码:A)
var overlay;
USGSOverlay.prototype = new google.maps.OverlayView();
// Initialize the map and the custom overlay.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {lat: -2.548926, lng: 118.0148634},
mapTypeId: google.maps.MapTypeId.roadmap
});
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-21.44555, -272.788888),
new google.maps.LatLng(16.776667, -212.22222));
var srcImage = 'http://localhost/komposat/no2.png';
overlay = new USGSOverlay(bounds, srcImage, map);
}
function USGSOverlay(bounds, image, map) {
// Initialize all properties.
this.bounds_ = bounds;
this.image_ = image;
this.map_ = map;
this.div_ = null;
this.setMap(map);
}
USGSOverlay.prototype.onAdd = function() {
var div = document.createElement('div');
div.style.borderStyle = 'none';
div.style.borderWidth = '0px';
div.style.position = 'absolute';
var img = document.createElement('img');
img.src = this.image_;
img.style.width = '100%';
img.style.height = '100%';
img.style.position = 'absolute';
div.appendChild(img);
this.div_ = div;
this.div_.style.opacity = 0.5;
var panes = this.getPanes();
panes.overlayLayer.appendChild(div);
};
var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
// Resize the image's div to fit the indicated dimensions.
var div = this.div_;
div.style.left = sw.x + 'px';
div.style.top = ne.y + 'px';
div.style.width = (ne.x - sw.x) + 'px';
div.style.height = (sw.y - ne.y) + 'px';
};
USGSOverlay.prototype.onRemove = function() {
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
};
google.maps.event.addDomListener(window, 'load', initMap);
我希望在用户点击地图时显示lat和lng位置。这是代码(代码:B)
google.maps.event.addListener(map, 'click', function( event )
{
alert( "Latitude: "+event.latLng.lat()+" "+", longitude: "+event.latLng.lng() );
});
我想在叠加地图中的任何地方显示lat和lng。我把我的代码:B放在代码中的initMap函数中:A,但它没有用。所以你们可以帮我解决这个问题吗?感谢。
答案 0 :(得分:0)
试试这个..如果它不起作用,那么在jsfiddle分享您的代码
google.maps.event.addListener(map, "click", function (event) {
var latitude = event.latLng.lat();
var longitude = event.latLng.lng();
console.log( latitude + ', ' + longitude );
alert(latitude + ', ' + longitude);
});