我有一个事件监听器,可以在点击时平移到地理定位标记的坐标。我点击它后,我希望标记略微偏向中心。是否可以在我的平底锅上添加这些坐标的lat来运行?这是我的代码:
for (let i = 0; i < someArray.length; i++){ geocoder.geocode( { address: someArray[i].full_address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK){ map.setCenter(results[0].geometry.location); let marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); google.maps.event.addListener(marker, 'click', function() { if (!infowindow) { infowindow = new google.maps.InfoWindow(); } infowindow.setContent(someArray[i].full_address); infowindow.open(map, marker); map.panTo(results[0].geometry.location); map.setZoom(14); });
例如,如果标记的坐标为(35.11111,-102.11111),我想在点击它时平移到(35.11511,-102.11111)。差不多,因为如果我点击带坐标(x,y)的标记它们会不断变化,我会平移到(x + .004,y)
仅限Javascript。
答案 0 :(得分:0)
将以下代码添加到click
功能并设置LatLng
var markerPos = new google.maps.LatLng(52.5243, 13.4105);
marker.setPosition(markerPos);
答案 1 :(得分:0)
如果要将0.004度添加到标记的纬度,请将0.004添加到标记的纬度,然后将其用于平移目标。
google.maps.event.addListener(marker, 'click', function() {
if (!infowindow) {
infowindow = new google.maps.InfoWindow();
}
infowindow.setContent("full_address");
infowindow.open(map, marker);
// add 0.004 to the marker latitude
var newLatLng = new google.maps.LatLng(this.getPosition().lat()+0.004, this.getPosition().lng());
map.panTo(newLatLng);
map.setZoom(14);
});
代码段
var geocoder;
var map;
var infowindow;
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 latLng = map.getCenter();
var marker = new google.maps.Marker({
map: map,
position: latLng
});
google.maps.event.addListener(marker, 'click', function() {
if (!infowindow) {
infowindow = new google.maps.InfoWindow();
}
infowindow.setContent("full_address");
infowindow.open(map, marker);
var newLatLng = new google.maps.LatLng(this.getPosition().lat() + 0.004, this.getPosition().lng());
map.panTo(newLatLng);
map.setZoom(14);
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
<div id="map_canvas"></div>