1

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.

Droidster
  • 127
  • 1
  • 2
  • 8
  • When you say without GPS do you mean not including any other LocationManager sources as well? – marcus.ramsden Nov 04 '12 at 00:12
  • yes please.. am noob sorry.. can u provide working code.. i try so much and all has errors 80% of time as google changes method names/etc.. urgh.. thank u =] – Droidster Nov 04 '12 at 00:25

2 Answers2

0

You can do this by obtaining the user's IP address and use a free GeoLocation API service such as the one here: http://freegeoip.net/static/index.html

These services work most of the time on Wifi, but are not fully reliable. In some circumstances, such as if the user is using a proxy, these services give a false (or inaccurate) location.

On 3G, these services can be wildly inaccurate. This is probably because the IP address assigned to phones is rooted at the network provider's central network location. Be very wary of making assumptions about the city when on 3G, country is probably the best you'll reasonably get.

HXCaine
  • 4,228
  • 3
  • 31
  • 36
0

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.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • I show error here: && Connectivity.isConnectedMobile(this.context)){ .... 'Connectivity is underlined in red stating - it can not be resolved, please advise =? – Droidster Nov 04 '12 at 00:57
  • That is a utility class I use for checking network connection. I posted it [here](http://stackoverflow.com/questions/2802472/detect-network-connection-type-on-android/8548926#8548926). – Emil Davtyan Nov 04 '12 at 00:59
  • Ok i got that .. code works flawless.. many thanks.. for the other folks i had to change 'isConnectedMobile' to 'isConnected' to not error. Now how do i post results to screen? What call for the class code do i put in my OnCreate() please? to make it work. =] – Droidster Nov 04 '12 at 01:04
  • You can use a `Toast`or something to display. Please upvote and accept the answer if it solved your issue. – Emil Davtyan Nov 04 '12 at 01:08
  • dont i need to call it from onCreate? does this do it: Intent intent = new Intent(); intent.setClass(mapView.getContext(), Locator.class); startActivity(intent); – Droidster Nov 04 '12 at 01:13
  • You can call it from anywhere, but honestly I think you need to [read this](http://mattgemmell.com/2008/12/08/what-have-you-tried/) before asking anymore questions. – Emil Davtyan Nov 04 '12 at 01:17