[英]Android GPS Location of the User
我需要獲取我的應用的“用戶位置”,以便可以在基於“緯度”和“緯度”兩個位置的Google Maps上顯示路線。
如果關閉了用戶GPS,則應詢問用戶是否要打開GPS,並應該能夠接受設置以將其打開。
我正在嘗試以下操作,但它直接將我帶到設置,我想知道如何讓用戶詢問他是否要被帶走。
是否有任何圖書館可以有效地做到這一點,我更喜歡使用它來獲取用戶的緯度和經度。
如果您要詢問如何詢問用戶是否對導航到設置頁面感興趣,為了打開位置服務,我建議您僅顯示一個對話框。 這是我的項目中的一個示例:
// Presents dialog screen - location services
private void askLocationDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(R.string.snoox_use_location_services_dialog);
// set dialog message
alertDialogBuilder.setMessage(R.string.would_you_like_to_turn_your_location_services_on_)
.setCancelable(false)
.setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// opens the setting android screen
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}
})
.setNegativeButton(R.string.no,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
alertDialogBuilder.create().show();
}
如果您對一個完整的示例感興趣,我認為這篇文章很有幫助: 如何確定是否啟用了Android設備的GPS
您將需要做幾件事。
首先,要整合Google Maps,請在此處獲得完整的參考。
簡單的步驟:
1按照以下步驟在這里 :這將有助於增加一個簡單的谷歌地圖到你的屏幕。
2為了能夠獲取自己的位置,您將需要在android中使用LocationListener和LocationManager。 為此,首先在您的活動中實現 LocationListener。
public class LocationActivity extends Activity implements LocationListener
3然后,您需要在onCreate()方法中實例化一些設置。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the provider
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
}
}
4您需要能夠請求常規位置更新。 將此包含在onResume()方法中。
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
5如果應用程序進入暫停周期,則不需要進行這些更新。
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
6您從步驟2開始的位置偵聽器實現要求您有一個onLocationChanged偵聽器,並將其實現:
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
}
7添加這兩種方法以通知您的位置設置提供商-GPS或網絡。
public void onProviderDisabled(String arg0) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String arg0) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
8現在,我們需要將其鏈接到您的Google地圖。 我將向您展示一個使用google maps API來生成市場以顯示您當前位置的示例。 可以從API推斷其他用法。
首先在您的代碼中創建私有字段:
private GoogleMap mMap;
Marker m;
9將它們添加到onCreate方法中-這將默認標記位置實例化為0,0緯度和經度。
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
m = mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0))
.title("Position"));
10在您的onLocationChanged方法中,我們需要在位置更改時刷新此標記。 因此添加:
m.setPosition(new LatLng(lat, lng));
m.setTitle("Your Position");
// Move the camera instantly to marker with a zoom
// of 15.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15));
這將是一種使用位置更新標記的簡單方法,並且應該是android中的Google Maps和location API的良好介紹。
要檢測GPS是否打開,可以使用@Dror提供的答案:)希望它能有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.