devfd/react-native-geocoder

get my current location city name

NijaahnandhRV opened this issue · 3 comments

Hi I have tried to get the current location city name but i cant get it. Can anyone help me on this issue?? I'm not at getting the response. Wrote this code in componentdidmount()

Code:

navigator.geolocation.getCurrentPosition(
          (position) => {
            try {
                Geocoder.geocodePosition({ position.coords.latitude, position.coords.longitude }.then(res =>
                     console.log(res)
            } catch(err) {
              console.log(err);
            }
          },
          (error) => this.setState({ error: error.message }),
          { enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
        );

@NijaahnandhRV - you are using the promise interface:

Geocoer.geocodePosition(...).then(...).catch(err => ...)

The other option is to use the async/await syntax:
const res = await Geocoder.geocodePosition(...)

const res = await Geocoder.geocodePosition(NY);

How come this doesn't work. It gives 10 outputs. How can I get the city name? See the debugged output below. I've tested in android samsung devices

var NY = {
  lat: 40.7809261,
  lng: -73.9637594
};
 currentArea = async() => {

  const res = await Geocoder.geocodePosition(NY);
  console.log('postion', res);

}

outputs:

position (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {streetName: "East Drive", feature: null, locale: "en_GB", locality: "New York", position: {…}, …} 1: {streetName: null, feature: "Central Park", locale: "en_GB", locality: "New York", position: {…}, …} 2: {streetName: null, feature: "Manhattan", locale: "en_GB", locality: "New York", position: {…}, …} 3: {streetName: null, feature: null, locale: "en_GB", locality: "New York", position: {…}, …} 4: {streetName: null, feature: "10028", locale: "en_GB", locality: "New York", position: {…}, …} 5: {streetName: null, feature: "New York County", locale: "en_GB", locality: "New York", position: {…}, …} 6: {streetName: null, feature: "New York-Northern New Jersey-Long Island, NY-NJ-PA", locale: "en_GB", locality: null, position: {…}, …} 7: {streetName: null, feature: "New York Metropolitan Area", locale: "en_GB", locality: null, position: {…}, …} 8: {streetName: null, feature: "New York", locale: "en_GB", locality: null, position: {…}, …} 9: {streetName: null, feature: "United States", locale: "en_GB", locality: null, position: {…}, …}length: 10__proto__: Array(0)

Hi @bkspace and @bbeckk
Thanks for your help as I'm getting the latitude and longitude from another api I can't use await and async for geocoder but I was able to fix this by another method.