Search code examples
androidionic-frameworkionic2cordova-pluginsgeocoder

Ionic Native Geocoder "Cannot find a location."


I am using the Native Geocoder in my Ionic app. Documented here

Ionic documentation

Github Repo

This is the method in my provider -

//This calls the Native geocoder and returns the geocoordinates. 
  getGeoCode(address: string): Promise<any> {
    console.log('The address key is - ' + address);
    return new Promise((resolve, reject) => {
      this.nativeGeocoder.forwardGeocode(address)
        .then((nativegeocoordinates: NativeGeocoderForwardResult) => {
          console.log('The coordinates are - ' + JSON.stringify(nativegeocoordinates));
          let addressCoordinates: DeliveryAddressCoordinates = nativegeocoordinates;
          resolve(addressCoordinates);
        })
        .catch((error: any) => {
          console.log(JSON.stringify(error));
          reject(error);
        });
    })
  }

The method accepts an address string as input. So I get the address from google places API, and pass the description here.

Now, for certain addresses, it just doesn't seem to find the coordinates, and returns a "Cannot find a location" message. For e.g. "Bhadra Brookefields Apartment B Block, Kundanahalli Lake Road, Kundalahalli, Brookefield, Bengaluru, Karnataka, India" is a valid address and returns the coordinates, while "Times Square 42 Street Station, New York, NY, USA" does not find a match and returns a message saying - "Cannot find a location".

For more information the "Cannot find a location" seems to be originating from the cordova plugin code here on line #182., where it is referencing the Android class.

Is this a known issue? Or is there something that I am doing wrong?


Solution

  • At least one other person believes the plugin seems to be a bit of a hit/miss - a comment on this article below

    Ionic 2 – Geolocation and GeocodingIonic 2 – Geolocation and Geocoding

    The comment is from one Mr. Purple Edge, and I cannot link the comment itself because it is a disq, and not allowed by Stack.

    So, I have resorted to what can be interpreted both as a solution or as a workaround. Instead of using the plugin, I am simply using the api, thus making a Http request to get the information.

    Method to get the information (in a provider):

     getGeoCodefromGoogleAPI(address: string): Observable<any> {
        return this._http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + address)
          .map(res => res.json());
      }

    Method call ->

    this._deliveryAddressService.getGeoCodefromGoogleAPI(event.description).subscribe(addressData => {
    
            let latitude: string = addressData.results[0].geometry.location.lat;
            let longitude: string = addressData.results[0].geometry.location.lng;
            }

    Of course, there needs to be the due process of registering for the key.