我正在使用Google地图,并且我编写了如下所示的JS代码,当用户通过浏览器授予位置实现许可权时,块许可,地图不显示任何东西,闻我点击它的面积,我得到以下错误:\谷歌地图错误“Uncaught TypeError:无法读取未定义的属性'x'
Erorr:
遗漏的类型错误:无法读取属性“X”的未定义
JS SCRIPT:
var pos = new Object();
var marker1 = new Object();
var marker2 = new Object();
var resp;
var loc;
var bus_loc;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
// zoom: 13
});
var infoWindow = new google.maps.InfoWindow({});
var scope = angular.element($("#address")).scope();
var angLoc;
scope.$apply(function() {
angLoc = scope.contact.business.location.split(',');
bus_loc = {
lat: parseFloat(angLoc[0]),
lng: parseFloat(angLoc[1])
};
});
var image = '/static/image/main_template/map/2.png';
marker2 = new google.maps.Marker({
map: map,
position: bus_loc,
icon: image
});
marker2.addListener('click', function() {
infoWindow.setPosition(bus_loc);
infoWindow.setContent('<p style="padding: 0; margin-top: 10px; direction: rtl">ساینا</p>');
infoWindow.open(map, marker2);
});
map.setCenter(bus_loc);
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
loc = {
lat: parseFloat(pos.lat),
lng: parseFloat(pos.lng)
};
var image = '/static/image/main_template/map/1.png';
marker1 = new google.maps.Marker({
map: map,
position: loc,
icon: image
});
marker1.addListener('click', function() {
infoWindow.setPosition(loc);
infoWindow.setContent('<p style="padding: 0; margin-top: 10px; direction: rtl">اینجا هستید</p>');
infoWindow.open(map, marker1);
});
map.setCenter(loc);
var distanceService = new google.maps.DistanceMatrixService();
distanceService.getDistanceMatrix({
origins: ['' + loc.lat + ',' + loc.lng + ''],
destinations: ['' + bus_loc.lat + ',' + bus_loc.lng + ''],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
durationInTraffic: true,
avoidHighways: false,
avoidTolls: false
},
function(response, status) {
if (status !== google.maps.DistanceMatrixStatus.OK) {
console.log('Error:', status);
} else {
resp = response;
$('#distance').html('<b>فاصله از شما: <b>' + resp.rows[0].elements[0].distance.text);
$('#duration').html('<b>زمان تخمینی رسیدن شما به این با خودرو: <b>' + resp.rows[0].elements[0].duration.text);
}
});
var bounds = new google.maps.LatLngBounds();
bounds.extend(marker1.getPosition());
bounds.extend(marker2.getPosition());
map.fitBounds(bounds);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
// infoWindow.setPosition(pos);
// infoWindow.setContent(browserHasGeolocation ?
// 'مکان جغرافیایی یافت نشد' :
// 'مرورگر شما امکان نشان دادن مکان جغرافیایی را ندارد.');
}
我该怎么办?
错误的行号? –
通常没有错误,但当我点击地图区域时: common.js:191未捕获TypeError:无法读取undefined属性'x' – altruistic
我的第一个想法是:bus_loc应该是“new google.maps.LatLng”,而不是只是一个对象,你使自己{lat:...,lng:...} –