安卓:获取用户当前的位置,而无需使用GPS或互联网 [英] Android: get current location of user without using gps or internet

查看:713
本文介绍了安卓:获取用户当前的位置,而无需使用GPS或互联网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得用户的当前位置,而无需使用 GPS 网​​络?我的意思是与移动网络提供商的帮助。谁能帮我?

Is it possible to get the current location of user without using gps or internet? I mean with the help of mobile network provider. Can anyone help me?

推荐答案

您正在寻找做的是使用得到的位置 LocationManager.NETWORK_PROVIDER 而不是 LocationManager.GPS_PROVIDER 。该 NETWORK_PROVIDER 将解决在GSM或WiFi,这永远可用。显然,与WiFi关闭,GSM将被使用。请记住,使用手机网络是精确到基本500米。

What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.

<一个href="http://developer.android.com/guide/topics/location/obtaining-user-location.html">http://developer.android.com/guide/topics/location/obtaining-user-location.html有一些真正伟大的资料和样品code。

http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.

在得到大部分的code。在的OnCreate做(),补充一点:

After you get done with most of the code in OnCreate(), add this:

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

您也可以有你的活动实施 LocationListener的类,从而实现 onLocationChanged()在您的活动。

You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity.

这篇关于安卓:获取用户当前的位置,而无需使用GPS或互联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆