如何将placeMarker函数添加到google.maps.Geocoder;
在此代码中,我单击地图并从地图中获取lat,lng并将其标记
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: {lat: 13.7245601, lng: 100.4930251}
});
map.addListener('click', function (event) {
placeMarker(event.latLng);
});
}
这是我的功能位置标记
function placeMarker(location) {
if (marker == null) {
marker = new google.maps.Marker({
position: location,
map: map
});
} else {
marker.setPosition(location);
}
$('#lat').val(location.lat())
$('#lng').val(location.lng())
}
这是可行的,但是当我尝试更改为选择的省份,地区后,谷歌地图将在地图中自动标记,如何添加功能侦听器,这也是我的代码
function codeAddress(){
var sub_dis = $('.sub_district_id').find(':selected').data('sub_dis');
var distri = $('.district_id').find(':selected').data('district');
var getProvince = $('.province_id').find(':selected').data('province');
var map = new google.maps.Map(document.getElementById('map'), {zoom: 12});
var geocoder = new google.maps.Geocoder;
geocoder.geocode({'address': getProvince+' '+distri+' '+sub_dis}, function(results, status) {
if (status === 'OK') {
map.setCenter(results[0].geometry.location);
new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
$('#lat').val(results[0].geometry.location.lat())
$('#lng').val(results[0].geometry.location.lng());
} else {
window.alert('Geocode was not successful for the following reason: ' +
status);
}
});
}
如何添加
map.addListener('click', function (event) {
placeMarker(event.latLng);
});
进入codeAddress()函数后,我尝试了多种方法,在找到区域后仍然无法单击。