【问题标题】:React-native GeolocationReact-native 地理定位
【发布时间】:2023-03-20 02:00:02
【问题描述】:

我是新来的反应。我期待使用 Cordova 插件从 Web-App 构建一个移动应用程序。 所以在这种情况下,我想获取 App 位置。我正在尝试使用 react-native Geolocation

我按照 Facebook 上的教程 here。但是当我尝试它时,它并没有显示我的位置。而是位置值从 unknown 更改为 {}

我在这里可能会犯一些错误。

  1. 我正在使用代理
  2. 在浏览器中进行测试,而不是在 Android 或任何其他本机设备中。
  3. Facebook 链接说它是 react-native 但我的项目是由我认为不是 React-native 的 create-react-app 创建的

如果其中任何一个不是导致此错误的原因,请帮助。

我的代码,

import React,{Component} from 'react';
class GeoLocation extends React.Component { 
	state = { initialPosition: 'unknown',
			  lastPosition: 'unknown', };

	 watchID: ?number = null; 

	 componentDidMount() { 
	 	if (navigator.geolocation) {
	 		navigator.geolocation.getCurrentPosition( (position) => { 
	 			var initialPosition = JSON.stringify(position); 
	 			this.setState({initialPosition}); 
	 		},
	 	 	(error) => alert(JSON.stringify(error)), {
	 	 		enableHighAccuracy: true, timeout: 20000, maximumAge: 1000
	 	 	} );
	 	  	
	 	  	this.watchID = navigator.geolocation.watchPosition((position) => {
	 	   		var lastPosition = JSON.stringify(position);
	 	    	this.setState({lastPosition});
	 	     });
	 	    } 
	 	}

	componentWillUnmount() {
	 navigator.geolocation.clearWatch(this.watchID);
	  } 

	  render() {
	   		return ( 
	   			<div className="main">
	   				<h1> Initial position:  {this.state.initialPosition} </h1>
	   	 			<h1> Current position: {this.state.lastPosition} </h1> 
	    		</div>
	   		); 
	   	}
}
export default GeoLocation;

【问题讨论】:

    标签: reactjs react-native geolocation


    【解决方案1】:

    HyperTrack,我们构建了一个定位服务,并包含一些 React 库和用于 React 和 Cordova 的示例应用程序,可以在我们的Github 找到。这可能会解决您在获取位置时遇到的问题,或者至少让您走上正轨。

    【讨论】:

    • 谢谢!看起来很酷,但我正在为学期项目做这个。所以买不起这种服务的钱。非常感谢。 :)
    • 顺便说一下,侧面导航栏中的链接似乎在 DOCS 页面中损坏了!我在阅读文档时遇到了一些问题!
    • @HelpingHand - 该服务可免费跟踪,在“沙盒”/测试模式下,您可以为多达 20 个用户执行无限操作。另外,哪些链接?
    • 对链接的混淆感到抱歉!这与我的浏览器有关。 :) 感谢您提供的信息!感觉服务不错,可以试一试。
    • 如果有什么可以帮助您的,请告诉我们@HelpingHand
    猜你喜欢
    • 2016-01-25
    • 2021-12-29
    • 2016-10-26
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    相关资源
    最近更新 更多