-1
我搜索了這個問題的答案近2個小時,但是我沒有找到解決辦法。事情是我想要使用設備GPS創建Google地圖活動,並且如果它關閉,請要求用戶激活它。我面臨的問題是每次我想打開活動時都會崩潰。我正在測試一個真實的設備。使用GPS時谷歌地圖活動崩潰
這裏是我的代碼:
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
public double latitude;
public double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla_hospitales_farmacias_cerca);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// // Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) buildAlertMessageNoGps();
// Get the name of the best provider
locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Get latitude of the current location
latitude = myLocation.getLatitude();
// Get longitude of the current location
longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Estás aquí").snippet("Farmacias y hospitales cercanos"));
// request that the provider send this activity GPS updates every 20 seconds
//locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
@Override
public void onLocationChanged(Location location) {
mMap.clear();
longitude = location.getLongitude();
latitude = location.getLatitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
// function that opened an activity to turn on the GPS
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("El GPS está desactivado. ¿Quiere activarlo? ")
.setCancelable(true)
.setPositiveButton("Sí.",
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
PantallaHospitalesFarmaciasCerca.this.finish();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
的事情是,如果我使用:
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
什麼,而不是我用它工作的時刻,但不與GPS,它的工作原理網絡位置。我怎樣才能使它與GPS一起工作? 非常感謝。
什麼是崩潰?請包括你的logcat。 – stkent 2014-12-27 18:54:46
好吧,我會試着看看我是否能從真實設備中獲取錯誤,但我不確定。 – Razvi 2014-12-27 18:56:21
那麼,如果我選擇日誌級別:錯誤,我會得到不同的錯誤,有時我會得到 12-27 19:59:01.788 883-883 /? E/AuthorizationBluetoothService:未啓用接近功能。 others 12-27 19:59:53.402 465-505 /? E/InputDispatcher:頻道'425b5a98吐司(服務器)'〜頻道無法恢復破壞,將被丟棄! 12-27 19:59:53.406 465-505 /? E/InputDispatcher:頻道'423e2d20 razvitrance.ownnavdrawertest/razvitrance.ownnavdrawertest.PantallaHospitalesFarmaciasCerca(服務器)'〜頻道無法恢復破碎,將被丟棄! – Razvi 2014-12-27 19:00:59