2017-05-02 12 views
1

私は緯度と経度に問題があります。座標を取得したいときは、24個のAPIしか座標を取得しません。他はしません。私は知らないなぜ位置座標を取得できませんか?緯度経度

私はGPSTrackerサービスを使用してい

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 

     // getting GPS status 
     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 
     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 
       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

       } 
       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(); 
        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS Enabled", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return location; 
} 

私はこの方法でDBにデータを保存しています:

あなたが私のDBを参照することができ、ここで
private void getCoordinates() { 
     GPSTracker gpstracker = new GPSTracker(this); 
     if(gpstracker.canGetLocation) 
     { 
      double locationLng = gpstracker.getLongitude(); 
      double locationLat = gpstracker.getLatitude(); 

      databaseReference.child("lng").setValue(locationLng); 
      databaseReference.child("lat").setValue(locationLat); 
     }else 
      gpstracker.showSettingsAlert(); 
    } 

、最初のユーザー25によって第2の24のAPIエミュレータ、によって保存されていた、他の人があまりにも動作しません:

db

答えて

0

あなたは23と25 APIの許可要求を必要とする

は、クラスにこれを追加し

private static final int PERMISSION_ACCESS_COARSE_LOCATION = 0; 

    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
       PERMISSION_ACCESS_COARSE_LOCATION); 
    } 
関連する問題