0
我有一個Android應用程序,它使用谷歌地圖,並具有顯示當前位置的功能。我收到了幾條評論,說「即使打開GPS,當前位置也不會出現。」我嘗試了幾個設備,但我看不到這個問題,但昨天我在手機上遇到了問題,它在早期工作的很好。即使GPS開啓但Wifi關閉,我仍然無法獲取當前位置,爲什麼?
,我想通了,如果是在當前位置不上來然後關閉Wi-Fi和使用移動互聯網獲取當前位置,並開始出現的解決方案。我知道這似乎有點不可思議,但它爲我工作。如果我錯過了一些東西,請告訴我我的代碼需要進行的更改。我無法爲我的應用用戶建議上述解決方法。
locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
cEnabled = getIntent().getExtras().getBoolean("cEnabled");
if (!enabled && !cEnabled) {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Would you like to activate GPS?\n(recommended- Yes)")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Activate GPS?");
// Icon for AlertDialog
alert.setIcon(R.drawable.location1);
alert.show();
}
ImageButton clocButton = (ImageButton) findViewById(R.id.cLoc);
clocButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
getToMyLocation();
return false;
}
});
private void getToMyLocation() {
mapView.invalidate();
Criteria criteria = new Criteria();
String provider = locMgr.getBestProvider(criteria, false);
Location lastLoc = locMgr.getLastKnownLocation(provider);
if(lastLoc != null) {
GeoPoint lastPoint = new GeoPoint((int)(lastLoc.getLatitude()*1E6), (int)(lastLoc.getLongitude()*1E6));
center = lastPoint;
showLocation(lastPoint);
}
else
{
Toast.makeText(mContext, "Waiting for current location", Toast.LENGTH_SHORT).show();
}
myLocOverlay.enableMyLocation();
myLocOverlay.runOnFirstFix(new Runnable() {
public void run() {
GeoPoint loc = myLocOverlay.getMyLocation();
mapView.getController().setCenter(loc);
center = loc;
}
});
}
private void showLocation(GeoPoint location) {
if (location != null) {
myLocOverlay.enableMyLocation();
mapView.getController().animateTo(location);
}
}
我已新增下列清單文件權限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
我設置默認的標準位置和尋找最佳供應商,但現在看來,字符串提供商= locMgr.getBestProvider(標準,FALSE);返回一個null提供者。但爲什麼? – piyush