【问题标题】:How to get the exact coordinates using a address in React-Native如何使用 React-Native 中的地址获取精确坐标
【发布时间】:2018-01-31 19:14:01
【问题描述】:

我使用 mapbox 在我的应用程序中显示地图,但我需要使用地址显示位置,所以我使用地理编码器将地址转换为坐标,但是当在地图中打印这些坐标时,我得到的这些坐标是错误的,地图给我显示另一个地方,在Android中还不错,但在IOS中很糟糕,有时地图显示在城市另一边的地方。我已经尝试过使用我知道是正确的坐标并且地图可以很好地显示它们,所以问题出在地理编码器上。

我尝试使用Google Maps API,但是写你地址的表格很奇怪我不明白

我可以使用什么库但可以得到准确的坐标?

【问题讨论】:

  • 尝试使用 Google Geocoding API,您可以使用此 API 发送地址以获取坐标或坐标以获取位置地址。 API 使用 GET 请求,速度也很快。
  • 但我需要准确的地址,例如“Cra 53#15-56, Cali”:maps.googleapis.com/maps/api/geocode/…..... 它给我带来了太多的价值,只有 2 个与我的城市相对应跨度>

标签: react-native location maps geocoder


【解决方案1】:

您可以使用 Geocoder 获取坐标,例如

this.watchId = navigator.geolocation.watchPosition(
                (position) => {
                    this.setState({
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude,
                        error: null,
                    });
                },

这个函数将返回坐标。 如果您获得了坐标,您可以调用 google API 服务来获取您街道的完整地址。

fetch('https://maps.googleapis.com/maps/api/geocode/json?address=' + lat + ',' + long + '&key=' + 'API_KEY')
        .then((response) => response.json())
            .then((responseJson) => {
            alert('error')
            alert('ADDRESS GEOCODE is BACK!! => ' + JSON.stringify(responseJson));
})

【讨论】:

  • 请尝试很好地格式化答案。此外,如果您能提及要导入哪些库,将会很有帮助。
猜你喜欢
  • 2017-12-22
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
相关资源
最近更新 更多