因此,我正在尝试制作自定义Google地图,用户可以使用输入(对地点使用自动填充建议)来选择地图中的位置或拖动标记以选择位置。
当我创建没有自动完成功能的地图时,地图上的标记仍然可以拖动。但是,一旦我添加自动完成侦听器,使用自动完成功能一次,标记就不再可以拖动。
这是我正在使用的JS代码:
defaultLatLong = {lat: xxxx lng: xxxx};
var map = new google.maps.Map(document.getElementById('map'), {
center: defaultLatLong,
zoom: 13,
mapTypeId: 'roadmap'
});
var input = document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds',map);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var marker = new google.maps.Marker({
map: map,
position: defaultLatLong,
draggable: true,
clickable: true
});
google.maps.event.addListener(marker, 'dragend', function(marker){
var latLng = marker.latLng;
currentLatitude = latLng.lat();
currentLongitude = latLng.lng();
var latlng = {lat: currentLatitude, lng: currentLongitude};
var geocoder = new google.maps.Geocoder;
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
input.value = results[0].formatted_address;
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
});
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
}
marker.setPlace({
placeId: place.place_id,
location: place.geometry.location
});
currentLatitude = place.geometry.location.lat();
currentLongitude = place.geometry.location.lng();
});
解决这个问题的任何方法?
看起来如果你使用标记的.setPlace()
方法它是不可拖动的。
请改用.setPosition()
方法。
更换:
marker.setPlace({
placeId: place.place_id,
location: place.geometry.location
});
有:
marker.setPosition(place.geometry.location);
代码段:
defaultLatLong = {
lat: 40.7127753,
lng: -74.0059728
};
var map = new google.maps.Map(document.getElementById('map'), {
center: defaultLatLong,
zoom: 13,
mapTypeId: 'roadmap'
});
var input = document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var marker = new google.maps.Marker({
map: map,
position: defaultLatLong,
draggable: true,
clickable: true
});
google.maps.event.addListener(marker, 'dragend', function(marker) {
var latLng = marker.latLng;
currentLatitude = latLng.lat();
currentLongitude = latLng.lng();
var latlng = {
lat: currentLatitude,
lng: currentLongitude
};
var geocoder = new google.maps.Geocoder;
geocoder.geocode({
'location': latlng
}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
input.value = results[0].formatted_address;
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
});
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
}
marker.setPosition(place.geometry.location);
currentLatitude = place.geometry.location.lat();
currentLongitude = place.geometry.location.lng();
});
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<input id="pac-input" />
<div id="map"></div>