2017-08-03 45 views
0

我已經做到了,所以它輸出的Lat & Lng當我在手機上全新安裝APK時,它會將它寫入5次,但在此之後,工作......我如何才能使其工作?無法讓android一直輸出Lat&Lng到Textview

在日誌中,它輸出我按下按鈕,但似乎沒有做任何事情。我測試的設備是銀河邊緣S7運行Android 7

下面的代碼

getLocation = (LocationManager) getSystemService(LOCATION_SERVICE); 
    listenLocation = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      townName.append(location.getLatitude() + " " + location.getLongitude()); 



     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent); 

     } 
    }; 
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      requestPermissions(new String[]{ 
        android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION, 
        android.Manifest.permission.INTERNET 
      }, 10); 
      return; 
     } 
    }else{ 
     configureButton(); 
    } 



} 

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){ 
    switch(requestCode){ 
     case 10: 
      if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
       configureButton(); 
      return; 
    } 
} 

private void configureButton() { 
    location.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view){ 
      getLocation.requestLocationUpdates("gps", 5000, 0, listenLocation); 
     } 
    }); 
} 
+0

使用'FusedLocationProviderAPI'用' GPSChecker' – Piyush

回答

0

使用貝洛奧裏藏特碼onLocationChanged()方法....

@Override 
public void onLocationChanged(Location location) { 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 

     LatLng latLng = new LatLng(latitude, longitude); 
     mMap.addMarker(new MarkerOptions().position(latLng).title("My Location")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(20)); 
    latLngString = location.getLatitude() + "," + location.getLongitude(); 
} 
相關問題