💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
在Xcode中打开 Info.plist ,右键,Add Row,增加一个key。 使用NSLocationWhenInUseUsageDescription 作为键名,使用 下列字符串作为键值: > PropertyFinder would like to use your location to find nearby > properties 添加完新值后,你的Plist文件将如下图所示: ![](https://box.kancloud.cn/2015-10-27_562f20d59f3b4.png) 这将在询问用户是否允许App使用他们的当前位置时,以这串文本作为提示信息。 打开 SearchPage.js, 找到TouchableHighlight 中渲染 ‘Location’ 按钮的代码,加入下列属性值: ~~~ onPress={this.onLocationPressed.bind(this)} ~~~ 这样,当点击Location按钮,会调用 onLocationPressed 方法。 在SearchPage 类中,增加方法: ~~~ onLocationPressed() { navigator.geolocation.getCurrentPosition( location => { var search = location.coords.latitude + ',' + location.coords.longitude; this.setState({ searchString: search }); var query = urlForQueryAndPage('centre_point', search, 1); this._executeQuery(query); }, error => { this.setState({ message: 'There was a problem with obtaining your location: ' + error }); }); } ~~~ navigator.geolocation可获取当前位置。这个方法定义在 Web API中,这对于曾经在浏览器中使用过位置服务的人来说并不陌生。React Native 框架用本地iOS服务重新实现了这个API。 如果当前位置获取成功,我们将调用第一个箭头函数,否则调用第二个箭头函数简单显示一下错误信息。 因为我们修改了plist文件,因此我们需要重新启动App,而不能仅仅是Cmd+R了。请在Xcode中终止App,然后重新编译运行App。 在使用基于地理定位的搜索之前,我们需要指定一个Nestoria数据库中的默认位置。在模拟器菜单中,选择Debug\Location\Custom Location … 然后输入 55.02的纬度和-1.42的经度。这是一个位于英格兰北部的非常优美的海边小镇,我的家。 ![](https://box.kancloud.cn/2015-10-27_562f20d5b43f4.png) 它远没有伦敦那么繁华——但住起来真的很便宜!:]