我正在获取地理位置。我遇到的问题是地理位置需要几秒钟才能响应。到那时,我的 ajax 已经在文档准备就绪时被调用,并且 long/lat 从未发布过。如何确保在地理定位完成之前不调用 ajax?
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
//get it another way
//else
var Longitude = '0';
var Latitude = '0';
}
function showPosition(position) {
var Longitude = position.coords.longitude;
var Latitude = position.coords.latitude;
}
$.ajax({
url: 'activities.php?action=log-campaign-visit',
type: 'post',
data: {
DeviceID: session,
CampaignId: id,
User: userid,
IP: IP,
Agent1: agent1,
Referer1: referer1,
CouponId: couponId,
CampaignMedium: Cmedium,
CampaignSource: source1,
URL: fullUrl,
RedemptionCode: RedemptionCode,
Userlanguage: userLang,
ScreenWidth: screenWidth,
ScreenHeight: screenHeight,
ScreenOrientation: orientation,
Longitude: Longitude,
Latitude: Latitude
},
success: function (data) {
$('#RedemptionCode').html(data);
if (data == 'Success') {}
},
done: function () {
alert("done");
},
fail: function () {
alert("error");
},
always: function () {
alert("complete");
},
});