react-native-geocoding 中的地理编码问题

问题描述

Error Image 我正在尝试从 react native 中的纬度和经度值中获取地址(反向地理编码)。但得到上述错误

 fetch('https://maps.googleapis.com/maps/api/geocode/json?address=' + 41.89 + ',' + 12.49 + '&key=' + myApiKey)
        .then((response) => response.json())
        .then((responseJson) => {
            console.log('ADDRESS GEOCODE is BACK!! => ' + JSON.stringify(responseJson));
        })
        .catch(error => console.log(error));

Geocoder.from(41.89,12.49)
            .catch(error => console.log(error))
            .then(json => {
                console.log("json",json);
                var addressComponent = json.results[0].address_components[0];
                console.log("addressComponent",addressComponent);
            });

属于 react-native-geocoding 报错

enter image description here

解决方法

您如何在代码中使用这些代码片段?

我尝试检查我的终端并且它工作正常。

以下是用于提取的 sample code 和代码片段:

import React,{ Component } from 'react';
import { Text,View,StyleSheet,Dimensions } from 'react-native';
import MapView from 'react-native-maps';

const styles = StyleSheet.create({
  container: {
    flex: 1,backgroundColor: '#fff',alignItems: 'center',justifyContent: 'center',},map: {
    width: Dimensions.get('window').width,height: Dimensions.get('window').height,});

class MapApp extends Component {
  componentDidMount = () => {
    fetch(
      'https://maps.googleapis.com/maps/api/geocode/json?address=' +
        41.89 +
        ',' +
        12.49 +
        '&key=' +
        'YOUR_KEY'
    )
      .then((response) => response.json())
      .then((responseJson) => {
        console.log(
          'ADDRESS GEOCODE is BACK!! => ' + JSON.stringify(responseJson)
        );
      })
      .catch((error) => console.log(error));
  };

  render() {
    return (
      <View style={styles.container}>
        <MapView
          style={styles.map}
          initialRegion={{
            latitude: 37.78825,longitude: -122.4324,latitudeDelta: 0.0922,longitudeDelta: 0.0421,}}>
        </MapView>
      </View>
    );
  }
}

export default MapApp;

这里是 react-native-geocoding 的 sample code 和代码片段:

import React,}}>
        </MapView>
      </View>
    );
  }
}

export default MapApp;