Need solution for obtaining city name w/o gps?


Ask by : Droidster November 04, 2012 01:08

Seems their were some updates to methods in this area as many existing examples do not work right...

Can someone show me how to get the name of the city from the network w/o gps and toast to screen?

It would be MUCH appreciated.

Thank you in advance.

View original question

Answer by : Emil DavtyanNovember 04, 2012 00:52

Get the latitude and longitude using NETWORK_PROVIDER, which won't use GPS. Here is a Locator utility class I wrote for my purposes, you are welcome to use and it modify it. It is working and tested in an app with minSdk of 7.

package com.emil;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/**
 * For determining location of user using various methods
 * 
 * @author Emil http://stackoverflow.com/users/220710/emil
 *
 */
public class Locator implements LocationListener {

    static private final int TIME_INTERVAL = 100; // minimum time betweeen updates in milliseconds
    static private final int DISTANCE_INTERVAL = 1; // minimum distance between updates in meters

    static public enum Method {
        NETWORK,
        GPS,
        NETWORK_THEN_GPS
    }

    private Context context;
    private LocationManager locationManager;
    private Locator.Method method;
    private Locator.Listener callback;

    public Locator(Context context) {
        super();
        this.context = context;
        this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    public void getLocation(Locator.Method method, Locator.Listener callback){
        this.method = method;
        this.callback = callback;
        switch(this.method){
        case NETWORK:
        case NETWORK_THEN_GPS:
            Location networkLocation = this.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if(networkLocation != null){
                this.callback.onLocationFound(networkLocation);
            }else{
                System.out.println("REQUEST UPDATES to NETWORK PROVIDER");
                this.requestUpdates(LocationManager.NETWORK_PROVIDER);
            }
            break;
        case GPS:
            Location gpsLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(gpsLocation != null){
                this.callback.onLocationFound(gpsLocation);
            }else{
                System.out.println("REQUEST UPDATES to GPS PROVIDER");
                this.requestUpdates(LocationManager.GPS_PROVIDER);
            }
            break;
        }
    }

    private void requestUpdates(String provider){
        if(this.locationManager.isProviderEnabled(provider)){
            if(provider.contentEquals(LocationManager.NETWORK_PROVIDER) 
                    && Connectivity.isConnected(this.context)){
                System.out.println("NETWORK CONNECTED, START LISTENING: "+provider);
                this.locationManager.requestLocationUpdates(provider, TIME_INTERVAL, DISTANCE_INTERVAL, this);
            }else if(provider.contentEquals(LocationManager.GPS_PROVIDER) 
                    && Connectivity.isConnectedMobile(this.context)){
                System.out.println("MOBILE NETWORK CONNECTED, START LISTENING: "+provider);
                this.locationManager.requestLocationUpdates(provider, TIME_INTERVAL, DISTANCE_INTERVAL, this);
            }else{
                System.out.println("PROPER NETWORK NOT CONNECTED : "+provider);
                this.onProviderDisabled(provider);
            }
        }else{
            this.onProviderDisabled(provider);
        }
    }

    public void cancel(){
        this.locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        System.out.println("LOCATION FOUND : "+location.getLatitude()+", "+location.getLongitude()+(location.hasAccuracy() ? " : +- "+location.getAccuracy()+" meters" : ""));
        this.locationManager.removeUpdates(this);
        this.callback.onLocationFound(location);
    }
    @Override
    public void onProviderDisabled(String provider) {
        System.out.println("PROVIDER DISABLED : "+provider);
        if( this.method == Locator.Method.NETWORK_THEN_GPS 
                && provider.contentEquals(LocationManager.NETWORK_PROVIDER) ){
            // Network provider disabled, try GPS
            System.out.println("REQUEST UPDATES to GSP PROVIDER, NETWORK PROVIDER DISABLED");
            this.requestUpdates(LocationManager.GPS_PROVIDER);
        }else{
            this.locationManager.removeUpdates(this);
            this.callback.onLocationNotFound();
        }
    }
    @Override
    public void onProviderEnabled(String provider) {
        // empty
        System.out.println("PROVIDER ENABLED : "+provider);
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // empty
        System.out.println("PROVIDER STATUS CHANGED : "+provider+" : STATUS : "+status);

    }

    public interface Listener {
        void onLocationFound(Location location);
        void onLocationNotFound();
    }

}

Make sure you add the appropriate permissions in your AndroidManifest.xml to use location providers.

Then use an API from say Google Maps or something to convert to city name.

View original answer