Abr*_*ram 5 javascript jquery google-maps google-maps-api-3
我想在标记点击上显示地图标记上的完全自定义信息窗口.我已经成功实现了这个答案,以获得一个div来显示地图画布点击...但我无法在标记点击上复制它.
是否可以在标记点击功能中获取标记像素位置,并抑制正常信息窗口显示所需的自定义信息窗口?
我试过这个:
google.maps.event.addListener(marker, 'click', function(args) {
var x=args.pixel.x+$('#map').offset().left; //we clicked here
var y=args.pixel.y;
info.style.left=x+'px';
info.style.top=y+'px';
info.style.display='block';
});
Run Code Online (Sandbox Code Playgroud)
但在控制台中,我看到:
Uncaught TypeError: Cannot read property 'x' of undefined
Run Code Online (Sandbox Code Playgroud)
geo*_*zip 18
MouseClickEvent唯一记录的属性是:
latLng LatLng事件发生时光标下方的纬度/经度.
要转换为像素位置使用的fromLatLngToContainerPixel方法MapCanvasProjection:
google.maps.event.addListener(marker, 'click', function (e) {
var point = overlay.getProjection().fromLatLngToDivPixel(e.latLng);
info.style.left = point.x + 'px';
info.style.top = point.y + 'px';
info.style.display = 'block';
});
Run Code Online (Sandbox Code Playgroud)
代码段:
var geocoder;
var map;
var overlay;
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
map: map,
draggable: true,
position: map.getCenter()
});
google.maps.event.addListener(map, 'projection_changed', function () {
overlay = new google.maps.OverlayView();
overlay.draw = function () {};
overlay.setMap(map);
var info = document.getElementById('myinfo');
google.maps.event.addListener(marker, 'click', function (e) {
var point = overlay.getProjection().fromLatLngToContainerPixel(e.latLng);
info.style.left = (point.x - 100)+ 'px';
info.style.top = (point.y) + 'px';
info.style.display = 'block';
});
google.maps.event.addListener(map, 'center_changed', function (e) {
var point = overlay.getProjection().fromLatLngToContainerPixel(marker.getPosition());
info.style.left = (point.x - 100)+ 'px';
info.style.top = (point.y) + 'px';
info.style.display = 'block';
});
google.maps.event.addListener(marker, 'drag', function (e) {
var point = overlay.getProjection().fromLatLngToContainerPixel(marker.getPosition());
info.style.left = (point.x - 100)+ 'px';
info.style.top = (point.y) + 'px';
info.style.display = 'block';
});
});
}
google.maps.event.addDomListener(window, "load", initialize);Run Code Online (Sandbox Code Playgroud)
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
div.info {
position: absolute;
z-index: 999;
width: 200px;
height: 50px;
display: none;
background-color: #fff;
border: 3px solid #ebebeb;
padding: 10px;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
<div id="myinfo" class="info">
<p>I am a div on top of a google map ..</p>
</div>Run Code Online (Sandbox Code Playgroud)
小智 5
以下是获取像素位置的方法:
function getPixelLocation(currentLatLng) {
var scale = Math.pow(2, map.getZoom());
// The NorthWest corner of the current viewport corresponds
// to the upper left corner of the map.
// The script translates the coordinates of the map's center point
// to screen coordinates. Then it subtracts the coordinates of the
// coordinates of the map's upper left corner to translate the
// currentLatLng location into pixel values in the <div> element that hosts the map.
var nw = new google.maps.LatLng(
map.getBounds().getNorthEast().lat(),
map.getBounds().getSouthWest().lng()
);
// Convert the nw location from geo-coordinates to screen coordinates
var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw);
// Convert the location that was clicked to screen coordinates also
var worldCoordinate = map.getProjection().fromLatLngToPoint(currentLatLng);
var currentLocation = new google.maps.Point(
Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale),
Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale)
);
console.log(currentLocation);
return currentLocation;
}
Run Code Online (Sandbox Code Playgroud)