需要获得城市名称w / o gps的解决方案吗?

时间:2012-11-04 00:07:39

标签: android

似乎他们对这个领域的方法进行了一些更新,因为许多现有的例子都不能正常工作......

有人可以告诉我如何从网络获取城市名称w / o gps和toast to screen?

非常感谢。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以通过获取用户的IP地址并使用免费的GeoLocation API服务(例如:http://freegeoip.net/static/index.html

这些服务大部分时间都在Wifi上工作,但并不完全可靠。在某些情况下,例如,如果用户使用代理,这些服务会提供错误(或不准确)的位置。

在3G上,这些服务可能非常不准确。这可能是因为分配给电话的IP地址植根于网络提供商的中央网络位置。在3G上做出关于城市的假设时要非常谨慎,国家可能是你合理得到的最好的。

答案 1 :(得分:0)

使用不会使用GPS的NETWORK_PROVIDER获取经度和纬度。 这是我为我的目的编写的Locator实用程序类,欢迎您使用它并对其进行修改。它在minSdk为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();
    }

}

请务必在AndroidManifest.xml中添加appropriate permissions以使用位置提供商。

然后使用来自Google Maps或其他内容的API转换为城市名称。