似乎他们对该领域的方法进行了一些更新,因为许多现有示例无法正常工作......
有人可以告诉我如何从没有 gps 的网络中获取城市名称并烤到屏幕上吗?
将不胜感激。
先感谢您。
似乎他们对该领域的方法进行了一些更新,因为许多现有示例无法正常工作......
有人可以告诉我如何从没有 gps 的网络中获取城市名称并烤到屏幕上吗?
将不胜感激。
先感谢您。
您可以通过获取用户的 IP 地址并使用免费的 GeoLocation API 服务来做到这一点,例如:http: //freegeoip.net/static/index.html
这些服务大部分时间都在 Wifi 上运行,但并不完全可靠。在某些情况下,例如如果用户使用代理,这些服务会给出错误(或不准确)的位置。
在 3G 上,这些服务可能非常不准确。这可能是因为分配给电话的 IP 地址植根于网络提供商的中心网络位置。在使用 3G 时,请不要对城市做出假设,国家可能是您可以合理获得的最佳选择。
使用 获取经纬度NETWORK_PROVIDER
,不使用 GPS。这是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
然后使用来自谷歌地图或其他东西的 API 转换为城市名称。