Javascript 由于与的连接不安全,对地理位置的访问被阻止ionic://localhost (关于cordova ios)
我正在尝试使用我创建的location.service获取移动设备的位置和方向指南针。它过去在android和ios上都能工作,现在在ios上,我可以得到位置,但设备定位有一个错误 [已阻止]由于连接不安全,对地理位置的访问被阻止 到ionic://localhost. 地理位置错误{代码:1,消息:来源没有 使用地理定位服务的权限 我已在*.plist文件中尝试了所有这些: 即时通讯使用: @angular/cli 7.3.9 科尔多瓦-lib@9.0.1 平台: 安卓8.1.0 ios 5.1.1 插件: cordova插件爱奥尼亚网络视图4.1.1 cordova插件爱奥尼亚网络视图 cordova插件地理定位4.0.2地理定位 我在cordova构建ios时,在我的iPhone ios 13.4.1上从Xcode运行它,并检查Safari的开发控制台: 地点.服务:Javascript 由于与的连接不安全,对地理位置的访问被阻止ionic://localhost (关于cordova ios),javascript,angular,cordova,compass-geolocation,cordova-ios,Javascript,Angular,Cordova,Compass Geolocation,Cordova Ios,我正在尝试使用我创建的location.service获取移动设备的位置和方向指南针。它过去在android和ios上都能工作,现在在ios上,我可以得到位置,但设备定位有一个错误 [已阻止]由于连接不安全,对地理位置的访问被阻止 到ionic://localhost. 地理位置错误{代码:1,消息:来源没有 使用地理定位服务的权限 我已在*.plist文件中尝试了所有这些: 即时通讯使用: @angular/cli 7.3.9 科尔多瓦-lib@9.0.1 平台: 安卓8.1.0 ios 5
import {Injectable} from '@angular/core';
import {BehaviorSubject, combineLatest, fromEvent, Observable, of, timer} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import {map, switchMap} from 'rxjs/operators';
import { Globals } from '../globals';
export interface DefiLocation {
accuracy: number;
altitude: number;
altitudeAccuracy: number;
heading: number;
latitude: number;
longitude: number;
speed: number;
compass: number;
}
@Injectable({
providedIn: 'root'
})
export class LocationService {
errorMessage$: string;
currentLocation$: BehaviorSubject<{
accuracy?: number,
altitude: number,
altitudeAccuracy: number,
heading: number,
latitude: number,
longitude: number,
speed: number
}> = new BehaviorSubject({
accuracy: 0,
altitude: 0,
altitudeAccuracy: 0,
heading: 0,
latitude: 32.5,
longitude: 35,
speed: 0,
});
currentCompass$: BehaviorSubject<number> = new BehaviorSubject(0);
currentCompass: number = 0;
currentPosition: {
accuracy: 0,
altitude: 0,
altitudeAccuracy: 0,
heading: 0,
latitude: 32.5,
longitude: 35,
speed: 0,
};
locationTimer;
sendLocationError: boolean = true;
constructor(public globals: Globals) {
this.isCurrentPosition$.next(true);
this.trackMe();
}
private trackMe() {
if (this.globals.iphone) {
window.addEventListener('deviceorientation', (event) => {
this.currentCompass$.next(Math.round(event['webkitCompassHeading']));
});
// }
} else {
window.addEventListener('deviceorientationabsolute', (event) => {
this.currentCompass$.next(360 - Math.round(event['alpha']));
}, true);
}
if (navigator.geolocation) {
this.locationTimer = timer(0, 100).subscribe(tick => {
navigator.geolocation.getCurrentPosition((position) => {
this.currentLocation$.next(position.coords);
this.isCurrentPosition$.next(true);
this.sendLocationError = true;
}, (err) => {
this.isCurrentPosition$.next(false);
this.sendError(err.message);
console.log(err);
this.errorMessage$ = err.message;
console.log(this.errorMessage$);
}, {
enableHighAccuracy: true
});
});
} else {
alert('Geolocation is not supported by this browser.');
this.sendError('navigator.geolocation = null ("Geolocation is not supported by this browser.")');
}
}
getCurrentLocation$(): Observable<DefiLocation> {
return combineLatest(this.currentCompass$.asObservable(), this.currentLocation$.asObservable())
.pipe(
map(([compass, location]) => {
return {
longitude: location.longitude,
latitude: location.latitude,
accuracy: location.accuracy,
altitude: location.altitude,
altitudeAccuracy: location.altitudeAccuracy,
heading: location.heading,
speed: location.speed,
compass
};
}
));
}
sendError(error) {
if (this.sendLocationError) {
this.sendLocationError = false;
window['cordova'].plugins.firebase.analytics.logEvent('user_location_failed', {param1: error});
setTimeout(function() { this.sendLocationError = true; }, 5000);
}
}
}
结果表明,当我修复位置时,错误消息与设备的位置无关。错误仍然存在。方向有点问题,但我有一个答案: 位置-当我在.plist文件中请求所有正确的权限时,我收到了一个正确的位置。关于权限。顺便说一句,我正在使用。 方位罗盘-根据: 在iOS上为DeviceMotionEvent和添加了权限API DeviceOrientationEvent a、 权限API-到目前为止,Chrome和Safari只显示了一条关于在没有用户手势的情况下询问设备方向的警告,但在ios 13+上,苹果又采取了另一个步骤并完全阻止了它。下面的最后一个代码示例中提供了询问预授权的正确方法示例 b、 旧的/新的选择-因为这种行为在应用程序中是非常不自然的,我尝试了一个使用本机资源的旧的不推荐的插件,它工作了,幸运的是,现在这个插件实际上是不可靠的 我的完整代码和回退 正在尝试从DeviceRady上的浏览器请求位置和方向的许可,该浏览器适用于android和ios。 如果没有,请尝试本机资源。 如果不是,则以ios13+web协议状态请求对用户手势的权限。 此运行在DeviceRady上:
private trackMe() {
// compass listener (also ios13+ fallback at "compassPremissioniOS13" funtion)
if (typeof DeviceOrientationEvent['requestPermission'] !== 'function') {
const deviceOrientationLestener = (event) => {
if (this.globals.iphone) {
this.currentCompass$.next(event['webkitCompassHeading']);
} else {
if (event.absolute) {
this.currentCompass$.next(360 - event.alpha);
} else {
window.removeEventListener('deviceorientation', deviceOrientationLestener);
window.addEventListener('deviceorientationabsolute', (eventB) => {
this.currentCompass$.next(360 - eventB['alpha']);
}, true);
}
}
};
window.addEventListener('deviceorientation', deviceOrientationLestener);
} else {
document.addEventListener('deviceready', () => {
navigator['compass'].watchHeading((head) => {
this.currentCompass$.next(head.trueHeading);
}, (error) => console.log(error), {frequency: 10});
}, false);
}
// GET LOCATION - energy saving and preformance inhancing for mobile, dumm and cyclic for desktop.
if (this.globals.cordova) {
document.addEventListener('deviceready', () => {
if (navigator.geolocation) {
navigator.geolocation.watchPosition((position) => {
this.currentLocation$.next(position.coords);
this.isCurrentPosition$.next(true);
this.sendLocationError = true;
}, (err) => {
this.isCurrentPosition$.next(false);
this.sendError(err.message);
this.errorMessage$ = err.message;
}, {
enableHighAccuracy: true
});
} else {
alert('Geolocation is not supported by this browser.');
this.sendError('navigator.geolocation = null ("Geolocation is not supported by this browser.")');
}
}, false);
} else {
if (navigator.geolocation) { // fallback for desktop testing
this.locationTimer = timer(0, 100).subscribe(tick => {
navigator.geolocation.getCurrentPosition((position) => {
this.currentLocation$.next(position.coords);
this.isCurrentPosition$.next(true);
this.sendLocationError = true;
}, (err) => {
this.isCurrentPosition$.next(false);
this.sendError(err.message);
this.errorMessage$ = err.message;
}, {
enableHighAccuracy: true
});
});
} else {
alert('Geolocation is not supported by this browser.');
this.sendError('navigator.geolocation = null ("Geolocation is not supported by this browser.")');
}
}
}
当用户按下导航按钮时,此操作将运行:
compassPremissioniOS13$() {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
if (typeof DeviceOrientationEvent['requestPermission'] === 'function') {
DeviceOrientationEvent['requestPermission']()
.then(permissionState => {
if (permissionState === 'granted') {
window.addEventListener('deviceorientation', (event) => {
this.currentCompass$.next(event['webkitCompassHeading']);
});
resolve('User accepted');
} else {
reject('User declined');
}
})
.catch(console.error);
}
} else {
alert('deviceorientation is not supported by this browser.');
this.sendError('deviceorientation = null ("deviceorientation is not supported by this browser.")');
}
});
}
你在这方面运气好吗?我使用的是纯Cordova no-Ionic,我在尝试升级到wkwebview时遇到了类似的错误,对地理位置的访问由于与的连接不安全而被阻止app://localhost.I 写下我得到的一切…@Jessepangburn谢谢!我的问题是在使用WKWebView之前,我不需要cordova插件地理定位插件添加这个插件解决了这个问题,尽管人们应该知道该插件有一个硬编码的5米距离过滤器,这与没有插件时navigator.geolocation.watchPosition的工作方式不同。