我正在尝试在我的应用程序中访问 navigator.geolocation。
我还在我的 info.plist 中设置了“NSLocationWhenInUseUsageDescription”。
但是,当我运行我的应用程序时,它会抛出一个错误,提示“未定义不是对象(评估 navigator.geolocation.getCurrentPosition)”。
这是我的代码 -
import React, { Component } from 'react';
import {
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
Navigator,
TouchableHighlight,
Alert,
TouchableOpacity,
View
} from 'react-native';
import Spinner from 'react-native-loading-spinner-overlay'
var GiftedSpinner = require('react-native-gifted-spinner');
var GLOBAL = require('./Globals');
var stateName;
var latitude;
var longitude;
class PersonByLocationPage extends Component {
constructor(props) {
super(props);
this.data = this.props.data;
navigator.geolocation.getCurrentPosition(
(position) => {
var crd = position.coords;
latitude = crd.latitude;
longitude = crd.longitude;
// alert(latitude);
// alert(longitude);
},
(error) => alert(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
this.REQUEST_URL = 'sample';
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false,
networkError: false,
};
}
有人能告诉我我做错了什么吗?
最佳答案
- 将 RCTGeolocation.xcodeproj 拖到 Libraries 文件夹中。
- 在项目导航器中点击您的项目。
- 点击“TARGETS”下的“YOUR_PROJECT_NAME”。
- 单击“构建阶段”选项卡。在“链接二进制文件与库”阶段,
- 单击加号图标并选择 libRCTGeolocation.a 从出现的模式列表中。
这应该就是您所需要的。让我知道这是否适合您。
关于react-native - React Native iOS 中的地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37996054/