2017-07-16 125 views
-1

我一直試圖通過gps通過以下代碼獲取位置。當我將提供者更改爲NETWORK_PROVIDER時,我得到更新,但是當我使用GPS_PROVIDER時,我沒有收到任何數據或位置信息。 我已經提到了以下教程 - http://www.viralandroid.com/2015/12/how-to-get-current-gps-location-programmatically-in-android.htmlhttps://www.youtube.com/watch?v=QNb_3QKSmMk在android中通過GPS訪問位置

package app.com.example.android.locationapp; 
import android.Manifest; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.provider.Settings; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity implements LocationListener { 
    TextView lat, longi; 
    LocationManager locationManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    lat = (TextView) findViewById(R.id.textView2); 
    longi = (TextView) findViewById(R.id.textView4); 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     Toast.makeText(this, "Please enable location permission", Toast.LENGTH_SHORT).show(); 
     return; 
    } else { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     Log.i("MainActivity", "reqLocationUpdates() method called"); 

    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.i("MainActivity","onLocation executed"); 
    lat.setText(location.getLatitude() + ""); 
    longi.setText(location.getLongitude() + ""); 
} 

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

@Override 
public void onProviderEnabled(String s) { 

} 

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

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    locationManager.removeUpdates(this); 
} 

}

+0

我建議使用FusedLocationProviderApi。這是更準確的,並會解決很多事情給你。 https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi –

+0

好吧,我也會檢查一下,但你能幫我找到這個代碼中的問題.. –

回答

0

請確保您有精確的位置信息的權限集。並確保您正在執行運行時權限檢查。但很可能你的問題是衛星的LOS。如果你在室內測試,你可能無法獲得衛星信號。沒有這一點,你無法弄清楚你的位置,它永遠不會給你打電話。去外面測試,看看它是否有效。

+0

是的我已經取得了良好的位置許可。而當我測試其他應用程序時能夠獲取正確的位置,所以存在一些不同的問題。 –