我正在创建一个应用程序,用于跟踪用户位置并在地图上标记多个标记。
虽然我的应用程序在浏览器上工作正常,但在英特尔XDK模拟器和移动设备中无效。
Cordova CLI版本:5.4.1 英特尔XDK版本:3240
下面是我的代码:
function currentpostionmap() {
$(document).on("pageshow", "#inside123", function () {
if (navigator.geolocation) {
function success(pos) {
userClat = pos.coords.latitude;
userClng = pos.coords.longitude;
var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
var myOptions = {
zoom: 15,
center: latlng,
disableDefaultUI: true,
zoomControl:true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
var image123 = 'https:///developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
// var bounds = new google.maps.LatLngBounds();
marker = new google.maps.Marker({
position: latlng,
map: map,
icon: image123
});
var infowindow1 = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow1.setContent('YOU');
infowindow1.open(map, marker);
map.setZoom(20);
map.setCenter(marker.getPosition());
}
})(marker));
multimarker(map, latlng, infowindow1);
}
function fail(error) {
var latlng = new google.maps.LatLng(18.5204, 73.8567);
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
marker = new google.maps.Marker({
position: latlng,
map: map
});
}
navigator.geolocation.getCurrentPosition(success, fail, {
maximumAge: 500000,
enableHighAccuracy: true,
timeout: 6000
});
}
});
}
function getlocation()
{
if (navigator != null) {
if (navigator.geolocation) {
function success(position) {
userClat = position.coords.latitude;
userClng = position.coords.longitude;
goToLogin();
}
function fail(error) {
if (error.code == PositionError.PERMISSION_DENIED) {
alert("App doesn't have permission to use GPS");
} else if (error.code == PositionError.POSITION_UNAVAILABLE) {
alert("No GPS device found");
} else if (error.code == PositionError.TIMEOUT) {
alert("Its taking too much time to find user location");
} else {
alert("An unknown error occured");
}
}
navigator.geolocation.getCurrentPosition(success, fail, {
maximumAge: 500,
enableHighAccuracy: true,
timeout: 600
});
}
}
else
{
console.log ('NAVIGATOR ERROR');
}
}
function multimarker(map, userloc, infowindow) {
jQuery.ajax({
url: baseurl + "getdriverlocation.php",
async: true,
success: function (data) {
data = $.trim(data);
if (data == "false") {
$('#footInfo').html('<p>No Driver Registered</p>');
} else {
// console.log(data)
var myArray = jQuery.parseJSON(data); // instead of JSON.parse(data)
jQuery(myArray).each(function (index, element) {
driverlat = element.driver_lat;
driverlng = element.driver_lng;
loginid = element.loginid;
locations.push([driverlat, driverlng, loginid])
});
var bounds1 = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
var latlng1 = new google.maps.LatLng(locations[i][0], locations[i][1]);
if (google.maps.geometry.spherical.computeDistanceBetween(latlng1, map.getCenter()) < 30000) {
drivermarker = new google.maps.Marker({
position: latlng1,
icon: "img/car.png",
map: map
});
drivermarker.setMap(map);
google.maps.event.addListener(drivermarker, 'click', (function (marker, i) {
return function (evt) {
// infowindow.setContent(locations[i][2]);
// infowindow.open(map, marker);
driverdetail(locations[i][2]);
}
})(drivermarker, i));
bounds1.extend(latlng1);
map.fitBounds(bounds1);
}
}
bounds1.extend(userloc);
map.fitBounds(bounds1);
// wait for the bounds change to happen
google.maps.event.addListenerOnce(map, 'bounds_changed', function () {
// set the center on the user
map.setCenter(userloc);
// wait for the center to change
google.maps.event.addListenerOnce(map, 'bounds_changed', function () {
// check to see if all the markers are still in bounds
if ((!map.getBounds().contains(bounds1.getNorthEast())) ||
(!map.getBounds().contains(bounds1.getSouthWest()))) {
// if not zoom out one level
// console.log(map.getZoom() + " zoom-1 will be " + (map.getZoom() - 1));
map.setZoom(map.getZoom() - 1);
}
});
});
}
}
});
}
google.maps.event.addDomListener(window, "load", currentpostionmap);
function watchDriverPosition(userid) {
$(document).on("pageshow", "#drivermain", function () {
if (navigator.geolocation) {
function success(position) {
window.setInterval(function () {
updateDriverLocation(position.coords.latitude, position.coords.longitude, userid);
}, 50000);
}
function fail(error) {
if (error.code == PositionError.PERMISSION_DENIED) {
alert("App doesn't have permission to use GPS");
} else if (error.code == PositionError.POSITION_UNAVAILABLE) {
alert("No GPS device found");
} else if (error.code == PositionError.TIMEOUT) {
alert("Its taking to find user location");
} else {
alert("An unknown error occured");
}
}
watchId = navigator.geolocation.watchPosition(success, fail, {
maximumAge: 500000,
enableHighAccuracy: true,
timeout: 6000
});
}
});
}
$(document).ready(function () {
console.log("ready!");
getlocation();
});
每当应用程序在模拟器上启动时,它首先会给我两个警报
寻找用户位置
第二是
发生未知错误
这是我的第一个移动应用程序,我真的很努力地达到这个目的,请在这方面帮助我。
答案 0 :(得分:0)
使用Intel XDK和Cordova时,必须使用适当的intel xdk库提供的某些脚本运行某些功能。
在地理位置的情况下,您必须使用此调用
调用它intel.xdk.geolocation.watchPosition(SUC,失败选项);
这是一个示例代码
https://software.intel.com/en-us/xdk/article/intel-xdk-geolocation-sample
请注意,例如,通过访问移动设备或相机上传图片需要类似的东西,因此在直接在浏览器上运行的html中,您可以访问相机或文件浏览器,但不能访问需要访问硬件的cordova和xdk打包应用程序。
希望这有助于你