【发布时间】:2018-03-23 10:09:51
【问题描述】:
我正在尝试做这样的事情:
我在屏幕右半边有表格,而标记在屏幕左侧居中。移动地图 - 标记将留在左侧的中心,我将在地图移动时从标记(可视化,但中心偏移)获得LatLng。
现在我在地图上设置了居中标记,我可以移动地图并从中心获取 LatLng。
这是执行此操作的脚本:
var map = null;
var marker;
function showlocation() {
if ("geolocation" in navigator) {
/* geolocation is available */
// One-shot position request.
navigator.geolocation.getCurrentPosition(callback, error);
} else {
/* geolocation IS NOT available */
console.warn("geolocation IS NOT available");
}
}
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};
function callback(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
document.getElementById('default_latitude').value = lat;
document.getElementById('default_longitude').value = lon;
var latLong = new google.maps.LatLng(lat, lon);
map.setZoom(16);
map.setCenter(latLong);
}
// google.maps.event.addDomListener(window, 'load', initAutocomplete);
function initAutocomplete() {
var mapOptions = {
center: new google.maps.LatLng(42.4405026181028, 19.24323633505709),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"),
mapOptions);
google.maps.event.addListener(map, 'center_changed', function () {
document.getElementById('default_latitude').value = map.getCenter().lat();
document.getElementById('default_longitude').value = map.getCenter().lng();
});
$('<div/>').addClass('centerMarker').appendTo(map.getDiv())
.click(function () {
var that = $(this);
if (!that.data('win')) {
that.data('win', new google.maps.InfoWindow({
content: 'this is the center'
}));
that.data('win').bindTo('position', map, 'center');
}
that.data('win').open(map);
});
我的标记以 CSS 为中心:
#map .centerMarker {
position: absolute;
/*url of the marker*/
background: url(http://maps.gstatic.com/mapfiles/markers2/marker.png) no-repeat;
/*center the marker*/
top: 50%;
left: 50%;
z-index: 1;
/*fix offset when needed*/
margin-left: -10px;
margin-top: -34px;
/*size of the image*/
height: 34px;
width: 20px;
cursor: pointer;
}
我只需要将所有这些从中心向左偏移 25%,并为表单保留 50% 的右侧屏幕。
【问题讨论】: