2017-02-03 46 views
0

我是新來的android開發,我嘗試了2個教程來獲取當前的位置,但他們都沒有爲我工作。我得到了準確的預期佈局,但不顯示位置,它似乎完全空白,就好像設置文本字段的命令不存在一樣。 logcat不顯示任何異常,這就是爲什麼我沒有發佈它。一切看起來很正常。 我認爲這可能是相關的權限,必須從Android 6.0開始實時運行,但我已經嘗試過的這個教程處理這個問題,除了沒有權限缺乏拋出安全異常? 這裏有兩個教程,我已經嚴格遵守,因爲它們是:android GPS位置不起作用?

http://demonuts.com/2016/12/30/get-current-location-android-studio/

http://www.viralandroid.com/2015/12/how-to-get-current-gps-location-programmatically-in-android.html

回答

0

對於映射到工作,你需要把google_maps_key到google_maps_api.xml這是價值觀的文件夾下。你在google api manager中獲得這個密鑰。

0

我使用設備內部的google地圖,通過意圖傳遞經緯度,然後單擊按鈕。

String uri = "geo:0,0?q="+ gps.getLatitude() + "," + gps.getLongitude() ;     
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setData(Uri.parse(uri)); 
      if (intent.resolveActivity(getPackageManager()) != null) { 
       startActivity(intent); 
      } 

我創建了一個TrackGPS類來幫助我獲得繩索。

public class TrackGPS extends Service implements LocationListener { 


    private final Context mContext; 


    boolean checkGPS = false; 


    boolean checkNetwork = false; 

    boolean canGetLocation = false; 

    Location loc; 
    double latitude; 
    double longitude; 


    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 


    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; 
    protected LocationManager locationManager; 

    public TrackGPS(Context mContext) { 
     this.mContext = mContext; 
     getLocation(); 
    } 

    private Location getLocation() { 

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

      // getting GPS status 
      checkGPS = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      checkNetwork = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!checkGPS && !checkNetwork) { 
       Toast.makeText(mContext, "No Service Provider Available", Toast.LENGTH_SHORT).show(); 
      } else { 
       this.canGetLocation = true; 
       // First get location from Network Provider 
       if (checkNetwork) { 
//     Toast.makeText(mContext, "Network", Toast.LENGTH_SHORT).show(); 

        try { 
         locationManager.requestLocationUpdates(
           LocationManager.NETWORK_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
//      Log.d("Network", "Network"); 
         if (locationManager != null) { 
          loc = locationManager 
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

         } 

         if (loc != null) { 
          latitude = loc.getLatitude(); 
          longitude = loc.getLongitude(); 
         } 
        } catch (SecurityException e) { 

        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (checkGPS) { 
//    Toast.makeText(mContext, "GPS", Toast.LENGTH_SHORT).show(); 
       if (loc == null) { 
        try { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
//      Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          loc = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (loc != null) { 
           latitude = loc.getLatitude(); 
           longitude = loc.getLongitude(); 
          } 
         } 
        } catch (SecurityException e) { 

        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return loc; 
    } 

    public double getLongitude() { 
     if (loc != null) { 
      longitude = loc.getLongitude(); 
     } 
     return longitude; 
    } 

    public double getLatitude() { 
     if (loc != null) { 
      latitude = loc.getLatitude(); 
     } 
     return latitude; 
    } 

    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    public void showSettingsAlert() { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

     alertDialog.setTitle("GPS Not Enabled"); 

     alertDialog.setMessage("Do you wants to turn On GPS"); 

     alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       mContext.startActivity(intent); 
      } 
     }); 


     alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
      } 
     }); 


     alertDialog.show(); 
    } 


// public void stopUsingGPS() { 
//  if (locationManager != null) { 
// 
// 
//   locationManager.removeUpdates(TrackGPS.this); 
//  } 
// } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 


} 

不要忘記要求GPS的權限。