我有一个带有标记的google地图和一个带有包含位置的选项的select。当用户选择一个选项时,从ajax成功请求中加载纬度和经度,最后google地图上的标记必须更改位置。
这是地图的脚本:
function initSubmitMap(_latitude,_longitude){
var mapCenter = new google.maps.LatLng(_latitude,_longitude);
var mapOptions = {
zoom: 7,
scrollwheel: false,
center: mapCenter,
disableDefaultUI: false,
//scrollwheel: false,
styles: mapStyles
};
var mapElement = document.getElementById('submit-map');
var map = new google.maps.Map(mapElement, mapOptions);
var marker = new MarkerWithLabel({
position: mapCenter,
map: map,
icon: 'assets/images/marker.png',
labelAnchor: new google.maps.Point(50, 0),
draggable: true
});
$('#submit-map').removeClass('fade-map');
google.maps.event.addListener(marker, "mouseup", function (event) {
var latitude = this.position.lat();
var longitude = this.position.lng();
$('#latitude').val( this.position.lat() );
$('#longitude').val( this.position.lng() );
});
/*google.maps.event.addDomListener(window, 'load', initialize);*/
//Autocomplete
var input = /** @type {HTMLInputElement} */( document.getElementById('address-map') );
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
google.maps.event.addListener(autocomplete, '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);
map.setZoom(17);
}
marker.setPosition(place.geometry.location);
marker.setVisible(true);
$('#latitude').val( marker.getPosition().lat() );
$('#longitude').val( marker.getPosition().lng() );
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
});
}
function newLocation(newLat,newLng)
{
map.setCenter({
lat : newLat,
lng : newLng
});
}
这是ajax请求的脚本:
$('select[name=governorat_id]').mouseup(function() {
var id = $(this).val();
$.ajax({
type: 'get',
url: "{{ URL::to('claim-gov-pos') }}",
data: {
'id': id
},
success:function(data){
$("#latitude").val(data.lat);
$("#longitude").val(data.lon);
newLocation(parseFloat(data.lat),parseFloat(data.lon))
}
});
});
我在函数newLocation中未定义错误映射。
答案 0 :(得分:1)
问题在于您的地图范围。
在init函数中,您将地图定义为局部变量,因此仅在该函数的范围内可用。
当您从newLocation调用它时-不再定义该变量。
看看:https://www.tutorialspoint.com/es6/es6_variables.htm
但是,为了快速修复,而不使用ES6语法(let / const等)或重新设计,只需像下面那样全局声明map
,我还添加了一些注释,您应该尝试捕获错误并进行处理它们-就像尝试捕获或未定义/空检查一样,只是一个想法!
var map, marker;
function initSubmitMap(_latitude,_longitude){
var mapCenter = new google.maps.LatLng(_latitude,_longitude);
var mapOptions = {
zoom: 7,
scrollwheel: false,
center: mapCenter,
disableDefaultUI: false,
//scrollwheel: false,
styles: mapStyles
};
var mapElement = document.getElementById('submit-map');
//this part does not declare a local variable.
map = new google.maps.Map(mapElement, mapOptions);
marker = new MarkerWithLabel({
position: mapCenter,
map: map,
icon: 'assets/images/marker.png',
labelAnchor: new google.maps.Point(50, 0),
draggable: true
});
$('#submit-map').removeClass('fade-map');
google.maps.event.addListener(marker, "mouseup", function (event) {
var latitude = this.position.lat();
var longitude = this.position.lng();
$('#latitude').val( this.position.lat() );
$('#longitude').val( this.position.lng() );
});
/*google.maps.event.addDomListener(window, 'load', initialize);*/
//Autocomplete
var input = /** @type {HTMLInputElement} */( document.getElementById('address-map') );
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
google.maps.event.addListener(autocomplete, '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);
map.setZoom(17);
}
marker.setPosition(place.geometry.location);
marker.setVisible(true);
$('#latitude').val( marker.getPosition().lat() );
$('#longitude').val( marker.getPosition().lng() );
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
});
}
function newLocation(newLat,newLng) {
//check map is not defined, try catch or null check???
map.setCenter({
lat : newLat,
lng : newLng
});
}
关于自动完成功能: