marlove / react-native-geocoding

MIT License
208 stars 54 forks source link

Unable to get proper address #48

Open MrFarhan opened 1 year ago

MrFarhan commented 1 year ago

Is there a way to get proper and in separated fields from address, i am using react-native-geocoding to get address from lat and lng but the issue is i am not getting proper address means zip is missing some times and sometimes other fields.

I am using following code to extract details from this npm, do we have any better approach to extract data ?

const data = response?.results?.[0];

export const mapAddressFormatter = data => {
    let customData = { address: '' };
    customData.address = data?.formatted_address || '';
    customData.geometry = data?.geometry;
    data?.address_components?.forEach(item => {
        if (item.types.includes('country')) {
            customData.country = item.long_name;
        }
        if (item.types.includes('point_of_interest')) {
            customData.county = item.short_name;
        }
        if (item?.types?.includes('postal_code')) {
            customData.postalCode = item.short_name;
        }
        if (
            item?.types?.includes('street_address') ||
            item?.types?.includes('route') ||
            item?.types?.includes('sublocality_level_1') ||
            item?.types?.includes('sublocality_level_2') ||
            item?.types?.includes('sublocality_level_3') ||
            item?.types?.includes('sublocality_level_4') ||
            item?.types?.includes('sublocality_level_5')
        ) {
            customData.streetAddress = item.long_name;
        }
        if (item?.types?.includes('street_number')) {
            customData.streetNumber = item.long_name;
        }
        if (item?.types?.includes('administrative_area_level_1')) {
            customData.state = item.short_name;
        }
        if (item?.types?.includes('locality')) {
            customData.city = item.short_name;
        }
    });

    return customData;
};

I need following fields from this npm by providing lat and long:

Note: I tried the famous places in my country but it doesn't return complete result.
MrFarhan commented 1 year ago

Is there any one?

STECHNO commented 10 months ago

The same issue is happening to me, however, it is not related to react-native-geocoding, as Google Place API doesn't return a postal code if it does not recognize an address as a real one

For example, if you pass latitude and longitude of any plain surface (which might be any playground, park, or any empty place) which is near any house, society, or building in this scenario, you will not get postal code, but if you move a bit and pass longitude and latitude of a building or house number which has possibly street number and house number, you will get postal code.

Try to pass a lat and lng of a valid address

Another workaround is you can do something like

 const latitude = centralLatitude + (Math.random() - 0.5) * offset;
 const longitude = centralLongitude + (Math.random() - 0.5) * offset;

Example for offset 0.01

To get a postal code, you can call APIs with random lat, lng (which should be close to your central lat, lng)

Make sure to use the least value in order to get nearby lat lng.