- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经阅读并测试了很多问题,但我仍然无法在 Android 上获得地理位置。
我使用 navigator.geolocation.getCurrentPosition,在 iOS 上一切正常,但在 Android 上,我对这个函数没有答案,无论是成功还是错误。
我已经安装了 react-native-permissions 以确保用户已经激活了权限,但它没有改变任何东西,因为它说一切都是“授权的”。
我注意到它来自设备的 GPS。如果我手动激活它,一切正常。但是,我找不到为用户激活它的方法。在 iOS 上,如果 GPS 未激活,我会陷入错误回调并告诉用户激活它,但在 android 上,什么都没有发生。
我不明白为什么我不能仅使用 getCurrentPosition 获得地理位置(我在 list 中有 ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION)。
这是我的代码的一部分:
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
//do my stuff with position value
},
(error) => {
Permissions.getPermissionStatus('location')
.then((response) => {
if (response !== "authorized") {
Alert.alert(
"Error",
"We need to access to your location",
[
{
text: "Cancel",
onPress: () => {
// do my stuff
}, style: 'cancel'
},
{
text: "Open Settings",
onPress: () => Permissions.openSettings()
}
]
);
} else {
// do my stuff
}
});
},
{ enableHighAccuracy: true, timeout: 2000, maximumAge: 1000 }
);
}
最佳答案
您应该需要启用 全局定位系统 在安卓上
为了在 Android 上启用位置/gps,我可以推荐这个模块:
https://github.com/Richou/react-native-android-location-enabler
它使用标准的 Android 对话框定位:
像这样
import React, { Component } from "react";
import { Text, StyleSheet, View, Platform } from "react-native";
import RNAndroidLocationEnabler from "react-native-android-location-enabler";
export default class index extends Component {
componentDidMount() {
this.getLocation();
}
onLocationEnablePressed = () => {
if (Platform.OS === "android") {
RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({
interval: 10000,
fastInterval: 5000,
})
.then((data) => {
this.getLocation();
})
.catch((err) => {
alert("Error " + err.message + ", Code : " + err.code);
});
}
};
getLocation = () => {
try {
navigator.geolocation.getCurrentPosition(
(position) => {
//do my stuff with position value
},
(error) => {
Permissions.getPermissionStatus("location").then((response) => {
if (response !== "authorized") {
Alert.alert("Error", "We need to access to your location", [
{
text: "Cancel",
onPress: () => {
// do my stuff
},
style: "cancel",
},
{
text: "Open Settings",
onPress: () => Permissions.openSettings(),
},
]);
} else {
// do my stuff
}
});
},
{ enableHighAccuracy: true, timeout: 2000, maximumAge: 1000 }
);
} catch (error) {
this.onLocationEnablePressed();
}
};
}
关于android - react native 地理位置getCurrentPosition无响应(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44586560/
这个问题已经有答案了: How do I return the response from an asynchronous call? (42 个回答) 已关闭 7 年前。 我正在尝试将用户当前位置置
这是我的: var position; navigator.geolocation.getCurrentPosition(onSuccess, onError); function on
我正在开发一款不断需要用户当前位置的应用程序。 我遇到了一个问题,即设备第一次获取用户当前位置的纬度和经度并将其缓存起来,我认为每次都返回相同的纬度和经度。有没有办法从设备中删除缓存位置?任何帮助将不
我一直在开发一个使用 getCurrentPosition() 的应用,但它在最新版本的 Chrome 中不再起作用,请参阅: Deprecating Powerful Features on Ins
我正在使用谷歌地理定位的 getCurrentPosition() 函数来获取用户的当前位置。 它在 Firefox 中运行良好,但在 chrome 中不起作用。 我的代码如下:: Click
我试图通过本地 IPv4 地址连接到本地计算机上托管的网站,通过 navigator.geolocation.getCurrentPosition 访问我的手机的地理位置。问题是,当从我的手机运行代码
我想从 getCurrentPosition() 方法获取坐标,以便可以在其他方法中使用它,但我无法做到这一点。 这是我的 HTML 这是我的 JavaScript 代码 var x = docum
我正在尝试初始化一个以当前用户位置为中心并带有标记的 map 。在本地,一切都很好,但是当我将 html 页面部署到 Google Appengine 时,它只显示 map ,没有地理定位....
我有一个关于 geolocation.getCurrentPosition() 的问题 我有以下代码: $(document).on('click', '#find_pickupStoresNearM
我正在使用 getCurrentPosition 获取本地纬度和经度。我知道这个函数是异步的,只是想知道如何返回可以被其他函数访问的纬度和经度值? function getGeo() {
我正在使用 android.media.MediaPlayer 在我的 Android 应用程序中播放歌曲。我的应用程序中的某些事件是在歌曲的某些部分触发的。要知道我现在在歌曲中的哪个位置,我使用 M
这个问题在这里已经有了答案: How do I return the response from an asynchronous call? (41 个回答) 关闭 7 年前。 我试图获取这些变量:
你能帮我解决这个问题吗?我有一个从 url 流式传输的搜索栏,我想在 textView 中显示 mediaplayer 的 currentTime,那么如何更新 textview 以每秒获取 curr
我的 cordova 版本是 6.3.1,我使用的是 cordova-plugin-geolocation 2.3.0。 在调用 navigator.geolocation.getCurrentPos
我尝试这个(为了在页面加载时获取当前用户位置) angular.module('ionicApp', ['ionic']) .controller('MyCtrl', function($scope)
我有这个 html 页面: Geolocation test navigator.geolocation.getCurrentPosition(functi
请教我如何解决 MediaPlayer 错误。我有 Mediaplayer、MediaController 和 listview。当我触摸列表时,歌曲会从 MediaController 开始,但是当
我遵循了 Igor Khrupin 关于使用 MediaPlayer 类在 Android 中流式传输 mp3 文件的教程:http://www.hrupin.com/2011/02/example-
我正在编写一个短音频持续时间(通常为 1-5 秒)的音频播放器,如下所示 //start media player mediaPlayer = new MediaPlayer(); mediaPlay
我正在使用一个 SeekBar 来显示音频文件的进度和搜索到某个时间。为了更新,我使用了一个 Runnable,它每隔一秒左右在 MediaPlayer 上调用 getCurrentPosition(
我是一名优秀的程序员,十分优秀!