[英]Android Google Maps API Location
我已經在Google上搜索了一下,但仍然找不到確切的答案。
目前在Android中使用Google Maps的“最佳”或“推薦”方式是什么?
我希望我的應用程序使用自動跟蹤用戶位置的藍點,就像單擊“我的位置”按鈕時的“地圖”應用程序一樣。 其中一些功能似乎只是通過使用地圖內置的,但是,我不確定如何從中提取位置,因為我已經閱讀過.getMyLocation()?
做這個的最好方式是什么?
我正在使用此代碼:
//get locationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
通過啟用“我的位置”圖層,可以用藍點顯示用戶的當前位置。 只需調用以下方法,就會在地圖上顯示顯示用戶當前位置的藍點:
mMap.setMyLocationEnabled(true);
為了讓您應該設置一個位置更新LocationListener
使用LocationClient
。 在實現LocationListener
接口時,您將覆蓋onLocationChanged
方法,該方法將為您提供位置更新。 當您使用LocationClient
請求位置更新時,您將創建一個LocationRequest
以指定您希望位置更新的頻率。
您必須使用位置監聽器。 它將解決您想要的所有問題。 使用此代碼。 我試圖用評論解釋所有事情。
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
public class YourActivity extends FragmentActivity implements
LocationListener, ConnectionCallbacks, OnConnectionFailedListener{
private GoogleMap mMap;
private LocationClient mLocationClient;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(1000) // 1 second - interval between requests
.setFastestInterval(16) // 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_map);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
setUpLocationClientIfNeeded();
//.. and connect to client
mLocationClient.connect();
}
@Override
public void onPause() {
super.onPause();
// if you was connected to client you have to disconnect
if (mLocationClient != null) {
mLocationClient.disconnect();
}
}
private void setUpMapIfNeeded() {
// if map did not install yet
if (mMap == null) {
// install it from fragment
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// if installation success
if (mMap != null) {
// init all settings of map
init();
}
}
}
//if client was not created - create it
private void setUpLocationClientIfNeeded() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(
getApplicationContext(),
this,
this);
}
}
private void init() {
// settings of your map
mMap.setBuildingsEnabled(true);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
// when you get your current location - set it on center of screen
@Override
public void onLocationChanged(Location location) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(9).bearing(0).tilt(0).build();
CameraUpdate cameraUpdate = CameraUpdateFactory
.newCameraPosition(cameraPosition);
mMap.animateCamera(cameraUpdate);
//Be careful, if want to show current location only once(after starting map) use this line
//but you don't need it in your task!
mLocationClient.removeLocationUpdates(this);
}
// after connection to client we will get new location from LocationListener
//with settings of LocationRequest
@Override
public void onConnected(Bundle arg0) {
mLocationClient.requestLocationUpdates(
REQUEST, //settings for this request described in "LocationRequest REQUEST" at the beginning
this);
}
@Override
public void onDisconnected() {
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
}
}
我最近兩天一直在尋找它。
回答您的問題:
目前在Android中使用Google Maps的“最佳”或“推薦”方式是什么?
今天,您有2個選擇:
據我所知,兩種方法都可以正常工作,但是如果您問我將使用哪一種,我會選擇一種Google推薦的方法:#1。
他們是這樣說的:
作為Google Play服務的一部分的Google Location Services API提供了更強大的高級框架,該框架可自動處理位置提供者,用戶移動和位置准確性。 它還根據您提供的功耗參數處理位置更新計划。 在大多數情況下,通過使用Location Services API,您將獲得更好的電池性能以及更合適的精度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.