Skip to content Skip to sidebar Skip to footer

Consultation About Show Location On Map By Street Name

How i can show location on map by street name using android and java ? For example: i'll type in my program street,city - and i get the location on map i have this sample: Intent i

Solution 1:

Use reverse-Geocoding with Geocoder class

Example for achieving coordinates of Empire State Building:

GeocodergeoCoder=newGeocoder(this, Locale.getDefault());    
        try {
            List<Address> addresses = geoCoder.getFromLocationName(
                "empire state building", 5);
            Stringadd="";
            if (addresses.size() > 0) {
                Stringcoords="geo:" + String.valueOf(addresses.get(0).getLatitude()) + "," + String.valueOf(addresses.get(0).getLongitude());
                Intenti=newIntent(android.content.Intent.ACTION_VIEW,
                             Uri.parse(coords));
                startActivity(i);
            }  
    } catch (IOException e) {

            e.printStackTrace();
        }

Solution 2:

Geocoder is great and thanks llya.

this query should be executed in a separate thread, for example, AsyncTask, here is a sample code, hope to be helpful.

publicclassAsyncTaskToQueryLocationextendsAsyncTask<String, Integer, LatLng> {

private WeakReference<Context> m_Context;
private WeakReference<UserLocationManager> m_Manager;
private WeakReference<OnGeoLocationQueryListener> m_Listener;
private Locale m_Locale;
private String m_ParsedLocation;

publicAsyncTaskToQueryLocation(Context context,
        UserLocationManager manager, OnGeoLocationQueryListener listener,
        Locale locale) {

    m_Context = new WeakReference<Context>(context);
    m_Manager = new WeakReference<UserLocationManager>(manager);
    m_Listener = new WeakReference<OnGeoLocationQueryListener>(listener);
    m_Locale = locale;
    m_ParsedLocation = null;
}

@Override
protected LatLng doInBackground(String... params) {

    Context context = m_Context.get();
    if (context == null) {
        returnnull;
    }

    if ((params == null) || (params.length == 0) || (params[0] == null)) {
        returnnull;
    }

    m_ParsedLocation = params[0];
    Geocoder geoCoder = new Geocoder(context, m_Locale);
    try {
        List<Address> addresses = geoCoder.getFromLocationName(
                m_ParsedLocation, 1);

        if (addresses.size() > 0) {
            returnnew LatLng(addresses.get(0).getLatitude(), addresses
                    .get(0).getLongitude());
        }
    } catch (IOException e) {

        e.printStackTrace();
    }

    returnnull;
}

@Override
protectedvoidonPostExecute(LatLng latLng) {

    OnGeoLocationQueryListener listener = m_Listener.get();
    if (listener != null) {
        listener.onGeoLocationQueryFinished(m_ParsedLocation, latLng);
    }

    onCancelled();
}

@Override
protectedvoidonCancelled() {

    UserLocationManager manager = m_Manager.get();
    if (manager != null) {
        manager.notifyAsyncTaskFinish(AsyncTaskToQueryLocation.class
                .getSimpleName());
    }

    clearReference();
}

privatevoidclearReference() {

    // release the weak reference.
    m_Listener.clear();
    m_Manager.clear();
    m_Context.clear();
}

}

Post a Comment for "Consultation About Show Location On Map By Street Name"