gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'getCurrentPosition' of undefined at geoLocationInit

转载 作者:行者123 更新时间:2023-11-30 20:20:07 30 4
gpt4 key购买 nike

尝试获取用户的当前位置时出现以下错误,这是我的控制台中显示的内容

jquery-3.3.1.min.js:2 Uncaught TypeError: Cannot read property 'getCurrentPosition' of undefined at geoLocationInit (google.js:8) at HTMLDocument. (google.js:5) at l (jquery-3.3.1.min.js:2) at c (jquery-3.3.1.min.js:2)

var map;
var myLatLng;
$(document).ready(function() {
geoLocationInit();

function geoLocationInit() {
if (navigator.geolocation) {
navigator.geoLocation.getCurrentPosition(success, fail);
} else {
alert("Browser not supported");
}
}

function success(position) {
console.log(position);
var Latval = position.coords.latitude;
var Lngval = position.coords.longitude;
myLatLng = new google.maps.LatLng(Latval, Lngval);
createMap(myLatLng);
}

function fail() {
alert("It Failed");
}
//Create Map
function createMap(myLatLng) {
var myLatLng = new google.maps.LatLng(0.3285284, 32.5738946);
map = new google.maps.Map(document.getElementById('map'), {
center: myLatLng,
zoom: 12
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
});
}

//Create marker
function createMarker(LatLng, icn, name) {
var marker = new google.maps.Marker({
position: LatLng,
map: map,
icon: icn,
title: name
});
}

//Nearby search
function nearbySearch(myLatLng, type) {
var request = {
location: myLatLng,
radius: '1500',
types: [type]
};
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);

function callback(results, status) {
//console.log(results);
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
LatLng = place.geometry.location;
icn = place.icon;
name = place.name;
createMarker(LatLng, icn, name);
}
}
}
}
});

最佳答案

只需将 navigator.geoLocation.getCurrentPosition 更改为 navigator.geolocation.getCurrentPosition

关于javascript - 未捕获的类型错误 : Cannot read property 'getCurrentPosition' of undefined at geoLocationInit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51534843/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com