0
votes

I want to display user current location. I am able to get the latitude and longitude of the user. Then I am using react-native-geocoding to get user location place name. But I am getting error when doing it.

Below is my code:

const getCity = () => {
    console.log("get city func");
    Geocoder.init("MY_API_KEY");
    Geocoder.from(28.6139, 77.2090).then(json => {
        var addressComponent = json.results[0].address_components[0];
        console.log(addressComponent);
    })
        .catch(error => console.warn(error));
}

<TouchableOpacity onPress={getCity}>
    <Text style={styles.title}>Location Screen</Text>
</TouchableOpacity>

I am getting error:

code: 4, message: "Error from the server while geocoding. The receive…s 'origin' field. Check it for more informations.", origin: {…}}
code: 4
message: "Error from the server while geocoding. The received datas are in the error's 'origin' field. Check it for more informations."
origin:
error_message: "This API project is not authorized to use this API."
results: []
status: "REQUEST_DENIED"
__proto__: Object
__proto__: Object

I don't know what I am doing wrong here. Also if there is any other way to get the user's location name then please give suggestions.

1

1 Answers

0
votes

Looks like an authorization problem, related to an invalid key. Try calling your Geocoder.init(API_KEY), at the root level of the file or application, not in the function definition itself.

Geocoder.init("MY_API_KEY");

const getCity = () => {
  console.log("get city func");
  Geocoder.from(28.6139, 77.2090).then(json => {
    var addressComponent = json.results[0].address_components[0];
    console.log(addressComponent);
  })
    .catch(error => console.warn(error));
}

<TouchableOpacity onPress={getCity}>
  <Text style={styles.title}>Location Screen</Text>
</TouchableOpacity>