Android Open Source - SmartNavi Geocoder Gisgraphy






From Project

Back to project page SmartNavi.

License

The source code is released under:

Apache License

If you think the Android project SmartNavi listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.osmdroid.bonuspack.location;
//  w  ww .jav  a2s  . c om
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.osmdroid.bonuspack.utils.BonusPackHelper;
import android.content.Context;
import android.location.Address;
import android.util.Log;

/**
 * Experimental. Implements an equivalent to Android Geocoder class, based on OpenStreetMap data and Gisgraphy service. <br>
 * 
 * Usage sample: http://services.gisgraphy.com/geocoding/geocode?address=nantes&country=fr&format=json
 * 
 * First feedback: 
 * 
 * pros: better tolerance than Nominatim to changes in wording (e.g. "street" instead of "avenue", "road", etc)
 * 
 * cons: 
 * - country parameter is currently mandatory => this is a major drawback. 
 * - lot of street names, hamlets, etc - that are displayed on standard OSM map - are not recognized at all, 
 * where Nominatim perfectly geocodes them. 
 * - there is no "maxresults/limit" parameter. 10 results are usually returned, a lot of them completely irrelevant. 
 * 
 * @see http://www.gisgraphy.com/documentation/user-guide.htm#geocodingservice
 * @author M.Kergall
 *
 */
public class GeocoderGisgraphy {

  public static final String GISGRAPHY_SERVICE_URL = "http://services.gisgraphy.com/";

  protected Locale mLocale;
  protected String mServiceUrl;

  public GeocoderGisgraphy(Context context, Locale locale){
    mLocale = locale;
    setService(GISGRAPHY_SERVICE_URL); //default service
  }
  
  public GeocoderGisgraphy(Context context){
    this(context, Locale.getDefault());
  }

  static public boolean isPresent(){
    return true;
  }
  
  /**
   * Specify the url of the service provider to use. 
   */
  public void setService(String serviceUrl){
    mServiceUrl = serviceUrl;
  }
  
  /** 
   * Build an Android Address object from the Gisgraphy address in JSON format. 
   */
  protected Address buildAndroidAddress(JSONObject jResult) throws JSONException{
    Address gAddress = new Address(mLocale);
    gAddress.setLatitude(jResult.getDouble("lat"));
    gAddress.setLongitude(jResult.getDouble("lng"));

    int addressIndex = 0;
    if (jResult.has("streetName")){
      gAddress.setAddressLine(addressIndex++, jResult.getString("streetName"));
      gAddress.setThoroughfare(jResult.getString("streetName"));
    }
    /*
    if (jResult.has("suburb")){
      //gAddress.setAddressLine(addressIndex++, jResult.getString("suburb"));
        //not kept => often introduce "noise" in the address.
      gAddress.setSubLocality(jResult.getString("suburb"));
    }
    */
    if (jResult.has("zipCode")){
      gAddress.setAddressLine(addressIndex++, jResult.getString("zipCode"));
      gAddress.setPostalCode(jResult.getString("zipCode"));
    }
    
    if (jResult.has("city")){
      gAddress.setAddressLine(addressIndex++, jResult.getString("city"));
      gAddress.setLocality(jResult.getString("city"));
    }
    
    if (jResult.has("state")){ //France: region
      gAddress.setAdminArea(jResult.getString("state"));
    }
    if (jResult.has("country")){
      gAddress.setAddressLine(addressIndex++, jResult.getString("country"));
      gAddress.setCountryName(jResult.getString("country"));
    }
    if (jResult.has("countrycode"))
      gAddress.setCountryCode(jResult.getString("countrycode"));
    
    return gAddress;
  }

  /**
   * Equivalent to Geocoder::getFromLocation(String locationName, int maxResults). 
   */
  public List<Address> getFromLocationName(String locationName, int maxResults)
  throws IOException {
    String url = mServiceUrl
      + "geocoding/geocode?"
      + "format=json"
      + "&country=" + mLocale.getLanguage()
      + "&address=" + URLEncoder.encode(locationName);
    Log.d(BonusPackHelper.LOG_TAG, "GeocoderGisgraphy::getFromLocationName:"+url);
    String result = BonusPackHelper.requestStringFromUrl(url);
    //Log.d(BonusPackHelper.LOG_TAG, result);
    if (result == null)
      throw new IOException();
    try {
      JSONObject jsonResult = new JSONObject(result);
      JSONArray jResults = jsonResult.getJSONArray("result");
      List<Address> list = new ArrayList<Address>(jResults.length());
      int n = Math.min(maxResults, jResults.length());
      for (int i=0; i<n; i++){
        JSONObject jResult = jResults.getJSONObject(i);
        Address gAddress = buildAndroidAddress(jResult);
        list.add(gAddress);
      }
      return list;
    } catch (JSONException e) {
      throw new IOException();
    }
  }
  
}




Java Source Code List

com.ilm.sandwich.BuildConfig.java
com.ilm.sandwich.Config.java
com.ilm.sandwich.GoogleMapActivity.java
com.ilm.sandwich.Info.java
com.ilm.sandwich.MySupportMapFragment.java
com.ilm.sandwich.OsmMapActivity.java
com.ilm.sandwich.Settings.java
com.ilm.sandwich.Smartgeo.java
com.ilm.sandwich.StartChooser.java
com.ilm.sandwich.TouchableWrapper.java
com.ilm.sandwich.helferklassen.HttpRequests.java
com.ilm.sandwich.helferklassen.Locationer.java
com.ilm.sandwich.helferklassen.MapDownload.java
com.ilm.sandwich.helferklassen.MyItemizedOverlay.java
com.ilm.sandwich.helferklassen.PlacesAutoComplete.java
com.ilm.sandwich.helferklassen.PlacesTextSearch.java
com.ilm.sandwich.helferklassen.Rechnung.java
com.ilm.sandwich.helferklassen.SuggestionsAdapter.java
org.osmdroid.bonuspack.BuildConfig.java
org.osmdroid.bonuspack.BuildConfig.java
org.osmdroid.bonuspack.cachemanager.CacheManager.java
org.osmdroid.bonuspack.clustering.GridMarkerClusterer.java
org.osmdroid.bonuspack.clustering.MarkerClusterer.java
org.osmdroid.bonuspack.clustering.StaticCluster.java
org.osmdroid.bonuspack.kml.ColorStyle.java
org.osmdroid.bonuspack.kml.IconStyle.java
org.osmdroid.bonuspack.kml.KmlDocument.java
org.osmdroid.bonuspack.kml.KmlFeature.java
org.osmdroid.bonuspack.kml.KmlFolder.java
org.osmdroid.bonuspack.kml.KmlGeometry.java
org.osmdroid.bonuspack.kml.KmlGroundOverlay.java
org.osmdroid.bonuspack.kml.KmlLineString.java
org.osmdroid.bonuspack.kml.KmlMultiGeometry.java
org.osmdroid.bonuspack.kml.KmlPlacemark.java
org.osmdroid.bonuspack.kml.KmlPoint.java
org.osmdroid.bonuspack.kml.KmlPolygon.java
org.osmdroid.bonuspack.kml.LineStyle.java
org.osmdroid.bonuspack.kml.Style.java
org.osmdroid.bonuspack.location.FlickrPOIProvider.java
org.osmdroid.bonuspack.location.GeoNamesPOIProvider.java
org.osmdroid.bonuspack.location.GeocoderGisgraphy.java
org.osmdroid.bonuspack.location.GeocoderNominatimOld.java
org.osmdroid.bonuspack.location.GeocoderNominatim.java
org.osmdroid.bonuspack.location.NominatimPOIProvider.java
org.osmdroid.bonuspack.location.POI.java
org.osmdroid.bonuspack.location.PicasaPOIProvider.java
org.osmdroid.bonuspack.mapsforge.GenericMapView.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileModuleProvider.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileProvider.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileSource.java
org.osmdroid.bonuspack.overlays.DefaultInfoWindow.java
org.osmdroid.bonuspack.overlays.ExtendedOverlayItem.java
org.osmdroid.bonuspack.overlays.FolderOverlay.java
org.osmdroid.bonuspack.overlays.GroundOverlay.java
org.osmdroid.bonuspack.overlays.InfoWindow.java
org.osmdroid.bonuspack.overlays.ItemizedOverlayWithBubble.java
org.osmdroid.bonuspack.overlays.MapEventsOverlay.java
org.osmdroid.bonuspack.overlays.MapEventsReceiver.java
org.osmdroid.bonuspack.overlays.MarkerInfoWindow.java
org.osmdroid.bonuspack.overlays.Marker.java
org.osmdroid.bonuspack.overlays.Polygon.java
org.osmdroid.bonuspack.overlays.Polyline.java
org.osmdroid.bonuspack.routing.GoogleRoadManager.java
org.osmdroid.bonuspack.routing.MapQuestRoadManager.java
org.osmdroid.bonuspack.routing.OSRMRoadManager.java
org.osmdroid.bonuspack.routing.RoadLeg.java
org.osmdroid.bonuspack.routing.RoadManager.java
org.osmdroid.bonuspack.routing.RoadNode.java
org.osmdroid.bonuspack.routing.Road.java
org.osmdroid.bonuspack.utils.BonusPackHelper.java
org.osmdroid.bonuspack.utils.DouglasPeuckerReducer.java
org.osmdroid.bonuspack.utils.HttpConnection.java
org.osmdroid.bonuspack.utils.PolylineEncoder.java
org.osmdroid.bonuspack.utils.WebImageCache.java