如何消除getjs currentpositionn and watchposition are deprecated

HTML5 的 geolocation 警告提示?_问答_ThinkSAAS
HTML5 的 geolocation 警告提示?
HTML5 的 geolocation 警告提示?
使用HTML5的geolocation时,chrome的控制提示如下:
getCurrentPosition() and watchPosition() are deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS. See
for more details.
这是什么意思?没看明白,求解答。
翻译一下:getCurrentPosition() 和 watchPosition()这两个方法在不安全的环境下不建议使用,在以后的规范可能不会支持。你应该考虑appliaction的安全性,比如使用https。详细情况请看
PS:因为获取位置信息,以及监控位置的变化这些操作都属于敏感性操作,所以browsers在执行都会非常谨慎。它需要你在安全的环境并且获取用户的同意才会执行。
添加你想要问的问题
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
官方1群:【已满】
让ThinkSAAS更好,把建议拿来。
关注微信,更好学习navigator.geolocation.getCurrentPosition fails on Android Browser - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
i'm trying to get the geolocation on Android Browser but nothing happens. I'm using a Samsung Galaxy S3 but i'm not sure about the version of my browser. Android version is 4.1.2
Here is my Code:
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
this is a code i copied and pasted from this site
it gives me the "navigator.geolocation"
but when it comes to "getCurrentPosition" my code stops working. Mobile Chrome works fine but this is not. I shared my position but still nothing happens. Any help will be appriciated.
Thanks everyone i found the solution,
i was getting the geolocation after some javascript operations. I tried to get the geolocation before document is ready. And it worked.
seems like PhoneGap has a problem with geolocation
I have the same issue
I'm using S3 with Android 4.1.2, phonegap geolocation feature doesn't work
user961496
In order to get the geolocation without errors, you have to make that code block work before using the values provided by the geolocation because operations are carried out asynchronously, in this question i found the solution by loading my geolocation script before other .js files. This solved my problem and another trick for this issue is, geolocation works more stable when you give "always" permission for browser to read your location. After loading for the first time, you never encounter geolocation errors.
Did you give Internet permission in manifest?
&manifest xlmns:android...&
&uses-permission android:name="android.permission.INTERNET"&&/uses-permission&
&/manifest&
8,17512445
I know this is a bit old but it keeps coming up on searches so I thought I would add a tip that helped me.
Because I want to get the location right as the page loads I found that I needed to introduce a very short delay after the page loads. When I had no delay, I would get no error but also I would not activate the location protocols on the phone. This half second delay solved the issue. You can play with the delay and see if it solves your issues.
setTimeout(function() {getAutoLocation(true)},500);
I get the location in my "getAutoLocation(true)" function. This setTimeout only exists to introduce the delay.
I found that some Android phones (old and new) don't run properly the function
getCurrentPosition, maybe trying to save some battery.
I played with the function watchPosition and then the high accuracy GPS kicked in.
Read this to know how to use the parameters properly:
In my case, this worked:
maximumAge: 0, timeout: 2000, enableHighAccuracy: true
Hope this helps someone.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledGeolocation
geolocation对象提供了对设备GPS传感器的访问。
这个API是基于W3C Geo location API Specification实现的。有些设备已经提供了对该规范的实现,对于这些设备采用内置实现而非使用PhoneGap的实现。对于没有地理位置支持的设备,PhoneGap的实现应该是完全兼容W3C规范。
对象(只读):
geolocation.getCurrentPosition
返回一个Position对象表示设备的当前位置。
navigator.geolocation.getCurrentPosition(geolocationSuccess,
[geolocationError],
[geolocationOptions]);
geolocationSuccess:获取位置信息成功时调用的回调函数,参数为当前的位置信息。
geolocationError:(可选项)获取位置信息出错时调用的回调函数。
geolocationOptions:(可选项)地理位置选项。
geolocation.getCurrentPositon是一个异步函数。它回传一个包含设备当前位置信息的Position对象给geolocationSuccess回调函数。如果发生错误,触发geolocationError回调函数并传递一个PositionError对象。
支持的平台:
BlackBerry (OS 4.6)
BlackBerry WebWorks (OS 5.0或更高版本)
Windows Phone 7 ( Mango )
简单的范例:
// 获取位置信息成功时调用的回调函数
// 该方法接受一个“Position”对象,包含当前GPS坐标信息
var onSuccess = function(position) {
alert('Latitude: '
+ position.coords.latitude
'Longitude: '
+ position.coords.longitude
'Altitude: '
+ position.coords.altitude
'Accuracy: '
+ position.coords.accuracy
'Altitude Accuracy: ' + position.coords.altitudeAccuracy
'Heading: '
+ position.coords.heading
+ position.coords.speed
'Timestamp: '
+ new Date(position.timestamp)
// onError回调函数接收一个PositionError对象
function onError(error) {
alert('code: '
+ error.code
'message: ' + error.message + '\n');
navigator.geolocation.getCurrentPosition(onSuccess, onError);
完整的范例:
Device Properties Example
监视设备的当前位置的变化。
var watchId = navigator.geolocation.watchPosition(geolocationSuccess,
[geolocationError],
[geolocationOptions]);
geolocationSuccess: 获取位置信息成功时调用的回调函数,参数为当前位置信息。
geolocationError:(可选项)获取位置信息出错时调用的回调函数。
geolocationOptions:(可选项)地理位置选项。
String:返回的watch id是位置监视String:返回的watch id是位置监视周期的引用。可以通过geolocation.clearWatch调用该watch ID以停止对位置变化的监视。
geolocation.watchPosition是一个异步函数。当检测到设备的位置发生改变时,它返回设备的当前位置。当设备检索到一个新的位置,会触发geolocationSuccess回调函数并传递一个Position对象作为参数。如果发生错误,会触发geolocationError回调函数并传递一个PositionError对象。
支持的平台:
BlackBerry (OS 4.6)
BlackBerry WebWorks (OS 5.0或更高版本)
Windows Phone 7 ( Mango )
简单的范例:
// 获取位置信息成功时调用的回调函数
// 该方法接受一个“Position”对象,包含当前GPS坐标信息
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: '
+ position.coords.latitude
'Longitude: ' + position.coords.longitude
'' + element.innerHTML;
// onError回调函数接收一个PositionError对象
function onError(error) {
alert('code: '
+ error.code
'message: ' + error.message + '\n');
// Options: 每隔3秒钟检索一次位置信息
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { frequency: 3000 });
完整的范例:
Device Properties Example
geolocation.clearWatch
停止watchID参数指向的设备位置变化监视。
navigator.geolocation.clearWatch(watchID);
watchID:要清除的watchPosition周期的id。(字符串类型)
geolocation.clearWatch函数通过清除watchID指向的geolocation.watchPosition来停止对设备位置变化的监视。
支持的平台:
BlackBerry (OS 4.6)
BlackBerry WebWorks (OS 5.0或更高版本)
Windows Phone 7 ( Mango )
简单的范例:
// 选项: 每隔3秒钟检索一次位置信息
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { frequency: 3000 });
// ...后继处理...
navigator.geolocation.clearWatch(watchID);
完整的范例:
Device Properties Example
geolocationSuccess
当得到一个有效地理位置信息时,此用户回调函数被调当获得一个地理位置信息时,此用户回调函数被调用。
function(position) {
// 进行处理
position:设备返回的地理位置信息。(Position类型)
function geolocationSuccess(position) {
alert('Latitude: '
+ position.coords.latitude
'Longitude: '
+ position.coords.longitude
'Altitude: '
+ position.coords.altitude
'Accuracy: '
+ position.coords.accuracy
'Altitude Accuracy: ' + position.coords.altitudeAccuracy
'Heading: '
+ position.coords.heading
+ position.coords.speed
'Timestamp: '
+ new Date(position.timestamp)
geolocationError
当geolocation函数发生错误时,此用户回调函数被调用。
function(error) {
// 处理错误
error:设备返回的错误信息。(PositionError类型)
geolocationOptions
用户定制地理位置检索的可选参数。
{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };
frequency:以毫秒为单位的检索位置周期。这个选项并非W3C规范的一部分,未来会被删除并用maximumAge来替代该选项。(数字类型)(默认值:10000)
enableHighAccuracy:提供一个表明应用程序希望获得最佳可能结果的提示。(布尔类型)
允许的以毫秒为单位的最大时间间隔,该时间间隔是从geolocation.getCurrentPosition或geolocation.watchPosition的调用到相应的geolocationSuccess回调函数被调用。(数字类型)
应用程序将接受一个缓存的位置信息,当该缓存的位置信息的年龄不大于此参数设定值,单位是毫秒。(数字类型)
Android的特异情况:
除非enableHighAccuracy选项被设定为true,否则Android 2.X模拟器不会返回一个地理位置结果。
{ enableHighAccuracy: true }
包含由geolocation API创建的Position坐标信息。
coords:一系列地理坐标。(Coordinates类型)
以毫秒为单位的coords的创建时间戳。(DOMTimeStamp类型)
Position对象是由PhoneGap创建和填充的,并通过一个回调函数返回用户。
支持的平台:
BlackBerry (OS 4.6)
BlackBerry WebWorks (OS 5.0或更高版本)
Windows Phone 7 ( Mango )
简单的范例:
// 获取位置信息成功后调用的回调函数
var onSuccess = function(position) {
alert('Latitude: '
+ position.coords.latitude
'Longitude: '
+ position.coords.longitude
'Altitude: '
+ position.coords.altitude
'Accuracy: '
+ position.coords.accuracy
'Altitude Accuracy: ' + position.coords.altitudeAccuracy
'Heading: '
+ position.coords.heading
+ position.coords.speed
'Timestamp: '
+ new Date(position.timestamp)
// onError回调函数接收一个PositionError对象
function onError(error) {
alert('code: '
+ error.code
'message: ' + error.message + '\n');
navigator.geolocation.getCurrentPosition(onSuccess, onError);
完整的范例:
Device Properties Example
iPhone的特异情况:
单位为秒而非毫秒。
var onSuccess = function(position) {
alert('Latitude: '
+ position.coords.latitude
'Longitude: ' + position.coords.longitude
'Timestamp: ' + new Date(position.timestamp * 1000)
PositionError
当发生错误时,一个PositionError对象会传递给geolocationError回调函数。
code:一个在下面常量列表中定义的错误代码。
message:说明错误细节的错误信息。
PositionError.PERMISSION_DENIED:权限被拒绝
PositionError.POSITION_UNAVAILABLE:位置不可用
PositionError.TIMEOUT:超时
当使用Geolocation发生错误时,一个PositionError对象会作为geolocationError回调函数的参数传递给用户。
Coordinates
一系列用来描述位置的地理坐标信息的属性。
latitude:以十进制表示的纬度。(数字类型)
longitude:以十进制表示的经度。(数字类型)
altitude:位置相对于椭圆球面的高度,单位为米。(数字类型)
accuracy:以米为单位的纬度和经度坐标的精度水平。(数字类型)
altitudeAccuracy:以米为单位的高度坐标的精度水平。(数字类型)
运动的方向,通过相对正北做顺时针旋转的角度指定。(数字类型)
speed:以米/秒为单位的设备当前地面速度。(数字类型)
作为Position对象的一部分,Coordinates对象是由PhoneGap创建和填充的。该Position对象会作为一个回调函数的参数返回用户。
支持的平台:
BlackBerry (OS 4.6)
BlackBerry WebWorks (OS 5.0或更高版本)
Windows Phone 7 ( Mango )
简单的范例:
// 获取位置信息成功后调用的回调函数
var onSuccess = function(position) {
alert('Latitude: '
+ position.coords.latitude
'Longitude: '
+ position.coords.longitude
'Altitude: '
+ position.coords.altitude
'Accuracy: '
+ position.coords.accuracy
'Altitude Accuracy: ' + position.coords.altitudeAccuracy
'Heading: '
+ position.coords.heading
+ position.coords.speed
'Timestamp: '
+ new Date(position.timestamp)
// 获取位置信息出错后调用的回调函数
var onError = function() {
alert('onError!');
navigator.geolocation.getCurrentPosition(onSuccess, onError);
完整的范例:
Geolocation Position Example
Android的特异情况:
altitudeAccuracy: Android设备上不支持该属性,返回值总是null。http - Cordova geolocation plugin getCurrentPosition deprecated - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
I have an ionic app that is trying to use geolocation exactly as shown in the docs.
var posOptions = {timeout: 10000, enableHighAccuracy: true};
$cordovaGeolocation.getCurrentPosition(posOptions)
.then(function (position) {
do something
}, function(err) {
console.log(err);
But now it has stopped working and in the console gives me this warning.
getCurrentPosition() and watchPosition() are deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS. See
for more details.
This seems like a huge change to the w3c spec I am just surprised there is not a lot of documentation regarding it. Can anyone tell me what I am missing here.
The app is running on phones so it's listening on localhost naturally. It is talking to the server over http not https but I don't see why that would affect getting geo-coordinates
I am testing the app on the browser and as a cordova app on an ios device.
I am using HTML5 geolocation directly. But cordova plugin is just angular wrapper over it, as they say .
Apparently browsers can not use geolocation from http pages any more. But for ionic this is issue only for livereload.
is some workaround described using http-proxy to have livereload working on https.
Running the app on device without livereload (i.e. 'ionic run android' without '-l' at the end) works fine.
Remember to run getCurrentPosition after deviceready event. For me this one works in one of the controllers:
document.addEventListener("deviceready", function () {
('deviceready fired!');
window.navigator.geolocation.getCurrentPosition(function(position) {
('Location from Cordova:');
("Latitude: " + position.coords.latitude + "; Longitude: " + position.coords.longitude);
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledFiled Under:
Update [October 31, 2011]
for the latest update in this saga!
Since Apple announced the
at WWDC a few weeks ago, I’ve been eagerly anticipating the addition of location services to Safari (Webkit).
Until now, providing an iPhone 3G user with a map and an indicator of their current position required building a native iPhone application in Objective-C and getting approval from Apple to distribute said app via the App Store. Not fun.
Well now that OS 3.0 is out, we can take advantage of Safari’s newly baked-in geolocation services and some other niceness to do the very same thing. ()
Native Geolocation
Safari now gives us navigator.geolocation from the
and all the magic that goes with it: getCurrentPosition (one time retrieval of location), watchPosition (continuously updated location), and clearWatch (stop watchPosition). (If you’re browsing with iPhone, Android, or Firefox 3.5 right now, take a peak at .)
We want the location marker to follow us around, so we’ll choose watchPosition. The following provides the basic format we’ll follow, though the final code will be different as we add the Google Maps juice:
function displayLocation(position){
var latitude = position.coords.latitude
var longitude = position.coords.longitude
// do something with this new information
}
&function handleError(error){
switch (error.code){
case error.PERMISSION_DENIED:
alert('Sorry. Permission to find your location has been denied.')
case error.POSITION_UNAVAILABLE:
alert('Sorry. Position unavailable.')
alert(error.code)
}
}
&navigator.geolocation.watchPosition(displayLocation, handleError)
When this code is run in an iPhone 3G with OS 3.0, the browser asks for permission to use your location. If permission is granted, the displayLocatio if not, the handleError function is called. Now let’s do something useful with this location information.
Google Maps
released a welcome upgrade to their mapping API this past spring. With the still developing API v3, they tossed out the API key, rewrote the code, improved the namespacing, and much more. In the interest of playing with all things new, we’ll use this version of Google Maps. And in the interesting of keeping the focus on the new geolocation shizzle, we’ll skip the in-depth Google Maps discussion and just cover some abstract goals.
Initially we’ll center the map somewhere in downtown Austin, Texas. Then, when our previously mentioned displayLocation function gets called, we’ll add a marker and re-center the map on that marker&just like the big kids do with their fancy native apps.
Inside displayLocation, we’ll create the marker just once and then update its position on subsequent calls. Every time the iPhone changes location, the marker moves to follow. (extra credit: if you want to create a breadcrumb trail, take out the if/else stuff to create a trail of new markers on each iteration)
Lastly, we add an onload attribute to the body element to invoke our geolocation and Google Maps magic after the page loads. We now have a cool mobile map that tracks your position as you’re moving.
CSS3 Animation
We could certainly stop here and pat ourselves on the back, but let’s apply one last bit of polish. If you look at the native iPhone Map app, you’ll notice that the blue location icon pulses in and out to keep your attention. Hmmm. Our current location marker is lovely PNG image with alpha transparency that smoothly overlays the background map but it doesn’t pulse. Unfortunately, we can’t animate PNGs. We could fade the image in and out with some more JavaScript coding, but let’s take a look at a better solution, compliments of the new Safari.
When the CSS animation draft was announced, it sounded rather odd to add behaviors (JavaScript’s fort&) to a styling spec (CSS). But given our current problem, we start to see the wisdom. We can select and animate the location marker with CSS3.
As demonstrated by , first we describe the effect with , then we select the marker element from the DOM and apply the animation with :
Update [July 1, 2009]
Google just changed their API so that our custom marker image (blue_dot_circle.png) is now a background-image for a div rather than a separate image element (img).
Therefore, our CSS selector needs to be modified so that we can pulse the right element. A quick change from #map_canvas img[src="blue_dot_circle.png"] to #map_canvas div[style*="blue_dot_circle.png"] in the code below solves the problem.
@-webkit-keyframes pulse {
opacity: 1.0;
40% {
opacity: 0.25;
100% {
opacity: 1.0;
}}#map_canvas div[style*=&blue_dot_circle.png&] {
-webkit-animation-name: pulse;
-webkit-animation-duration: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-in-out;}
It’s a simple solution that provides the effect we need without the added overhead of JavaScript.
Feel free to
with your normal browser, you’ll get a plain old map of Austin&not much more. For the real show, open it in iPhone 3G and find your current location. Then start moving.
When you get back to the desk, crack open the source code to see how it was all brought together.
So there it is: a breezy introduction to what is possible with the iPhone’s new Safari geolocation powers and Google Maps. We certainly don’t have to stop here. We haven’t even mentioned that other mobile devices have or will have these very same powers. That web developers now have direct access to these powers means that we can expect a torrent of location-aware innovation.
See you at the .
Reference Links
Update [February 24, 2010]
for a fix for the phantom marker issue noted in the comments.
Update [April 2, 2010]
Make sure you also download the
and place it in the same directory as your map page. Sorry, I should have been more clear about that.
Update [October 31, 2011]
for the latest update in this saga!
an experimental site in design flux plebeosaur.us beta

我要回帖

更多关于 deprecated 的文章

 

随机推荐