2014-12-24 3 views
2

мой код здесь:Невозможно получить координату без wifi?

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    List<String> providers = lm.getProviders(false); 
    Location l = null; 

    for (int i=providers.size()-1; i>=0; i--) { 
      l = lm.getLastKnownLocation(providers.get(i)); 
      if (l != null) break; 
    } 

    double[] gps = new double[2]; 
    if (l != null) { 
      gps[0] = l.getLatitude(); 
      gps[1] = l.getLongitude(); 
    } 
    return gps; 

Это только получить координату, когда я подключен к интернету.

+0

см. Http://stackoverflow.com/questions/14499771/how-to-get-the-current-accurate-gps-coordinates-using-gps-provider-in-android –

ответ

0

Вы можете использовать андроида обслуживание GPS, чтобы получить Longitute и Latitute тоже,

добавить разрешение в вашем AndroidMenifest.xml,

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Initialization,

// saving the context for later use 
    private final Context mContext; 

    // if GPS is enabled 
    boolean isGPSEnabled = false; 

    // Location and co-ordinates coordinates 
    Location mLocation; 
    double mLatitude; 
    double mLongitude; 

    // Minimum time fluctuation for next update (in milliseconds) 
    private static final long TIME = 30000; 
    // Minimum distance fluctuation for next update (in meters) 
    private static final long DISTANCE = 20; 

    // Declaring a Location Manager 
    protected LocationManager mLocationManager; 

    // This is constructor to get System Location services, 
    public GPSService(Context context) { 
     this.mContext = context; 
     mLocationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 

    } 

Проверка GPS удалось извлечь данные,

try { 

      // Getting GPS status 
      isGPSEnabled = mLocationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // If GPS enabled, get latitude/longitude using GPS Services 
      if (isGPSEnabled) { 
       mLocationManager.requestLocationUpdates(
         LocationManager.GPS_PROVIDER, TIME, DISTANCE, this); 
       if (mLocationManager != null) { 
        mLocation = mLocationManager 
          .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
        if (mLocation != null) { 
         mLatitude = mLocation.getLatitude(); 
         mLongitude = mLocation.getLongitude(); 
        } 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
Смежные вопросы