【问题标题】:How to get latitude and longitude of my current location in android ? (Not with GPS but with internet service) [duplicate]如何在android中获取我当前位置的纬度和经度? (不使用 GPS,但使用互联网服务)[重复]
【发布时间】:2015-06-04 14:17:54
【问题描述】:

我想在 android 中获取我的确切纬度和经度。我的安卓设备中没有 GPS。所以,我需要借助互联网获取我的位置。

&

如果获取纬度和经度失败,则显示“获取位置失败”的吐司

提前致谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    尝试网络提供商

        LocationManager lManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    
             boolean netEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                    if (netEnabled) {
                        lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                        location = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      if (location != null) 
                    {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
    
                            }
                    }
    

    【讨论】:

    • 您好 +Dilavar ,是否需要安装 Google Play 服务才能使用它。请回答。
    • 不使用Google play服务。
    【解决方案2】:

    老答案 尝试使用此代码。它会通过网络而不是 GPS 为您提供位置

    Location location =myManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    latPoint=location.getLatitude();
    lngPoint=location.getLongitude();
    

    更新的 ANS

    private Location getLastBestLocation() {
    Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
    long GPSLocationTime = 0;
    if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); }
    
    long NetLocationTime = 0;
    
    if (null != locationNet) {
        NetLocationTime = locationNet.getTime();
    }
    
    if ( 0 < GPSLocationTime - NetLocationTime ) {
        return locationGPS;
    }
    else {
        return locationNet;
    }
    }
    

    【讨论】:

    • 你好,Umair Khalid。感谢您的回答。我可以做些什么来配置:如果 GPS 可用,则从 GPS 获取位置,否则使用网络。再次感谢。
    • getLastKnownLocation() 存储最佳最后位置。如果您不将您的位置更改为下一个点,那么它将为您提供它存储的最后一个已知位置。干杯!
    【解决方案3】:

    你应该这样做而不是LocationManager.GPS_PROVIDER,使用LocationManager.NETWORK_PROVIDER

    LocationManager lManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(
                                    LocationManager.NETWORK_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("Network", "Network");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                    System.out.println("latitude network" + latitude+"longi "+location.getLongitude());
                                    if (latitude == 0.0) {
                                        showSettingsAlertData();
                                    }
                                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2011-09-29
      • 1970-01-01
      • 2011-08-31
      • 2012-04-24
      • 1970-01-01
      • 2017-05-09
      相关资源
      最近更新 更多