首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当android中的Internet(wifi/DataPack)打开时,如何获取用户的位置

当android中的Internet(wifi/DataPack)打开时,如何获取用户的位置
EN

Stack Overflow用户
提问于 2015-10-07 13:01:55
回答 3查看 152关注 0票数 1

我试图获得的位置只有当用户互联网是oN,而不是使用全球定位系统/网络提供商。

EN

回答 3

Stack Overflow用户

发布于 2015-10-07 13:08:09

你要做的是使用LocationManager.NETWORK_PROVIDER而不是LocationManager.GPS_PROVIDER来获得这个职位。NETWORK_PROVIDER将在GSM或wifi上解析。显然,在关闭wifi的情况下,将使用GSM。请记住,使用蜂窝网络基本上可以精确到500米。

http://developer.android.com/guide/topics/location/obtaining-user-location.html**

**有一些非常棒的信息和示例代码。**

在完成OnCreate()中的大部分代码之后,添加以下代码:

代码语言:javascript
复制
// 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()。

票数 1
EN

Stack Overflow用户

发布于 2015-10-07 13:04:55

尝尝这个。

代码语言:javascript
复制
    public void getCurrentLocation() {

        if (mlocManager != null) {
            mlocManager .requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0, locListener );
            mlocManager .requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0, locListener );
        }

}
票数 0
EN

Stack Overflow用户

发布于 2015-10-07 13:19:48

请尝试使用互联网获取当前位置。

代码语言:javascript
复制
public class CurrentLocation extends Activity {

TextView tv;
Geocoder geocoder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_current_location);

    tv = (TextView) findViewById(R.id.textView1);
    geocoder = new Geocoder(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.

            String text = String.format(
                    "Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f",
                    location.getLatitude(), location.getLongitude(),
                    location.getAltitude(), location.getBearing());
            tv.setText(text);

            try {


                List<Address> addressList = geocoder.getFromLocation(
                        location.getLatitude(), location.getLongitude(), 1);

                Log.d("Current", addressList.toString());
                if (addressList != null && addressList.size() > 0) {
                    Address address = addressList.get(0);
                    StringBuilder sb = new StringBuilder();

                    String address_own = addressList.get(0).getAddressLine(
                            0);
                    String city = addressList.get(0).getAddressLine(1);
                    // String country = addresses.get(0).getAddressLine(2);

                    Log.d("Address", address_own);
                    Log.d("City", city);
                    // Log.d("Country", country);

                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                        sb.append(address.getAddressLine(i)).append("\n");
                    }

                    tv.append("\n" + address_own + ", " + city);
                }



            } catch (IOException e) {
                Log.e("LocateMe", "Could not get Geocoder data", e);
            }

        }

        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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32984134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档