1
我在我的Android應用程序中使用地圖,但我想使用位置更新並在地圖上顯示它,我正在跟蹤谷歌開發人員有關使用LocationRequest()位置更新的文檔,但不知道如何在地圖中使用它。 這裏是我的activity.java谷歌地圖API位置更新
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
public class MapaActivity extends FragmentActivity implements OnMapReadyCallback,ConnectionCallbacks,OnConnectionFailedListener, LocationListener {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private GoogleApiClient mGoogleApiClient;
// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1001;
// Unique tag for the error dialog fragment
private static final String DIALOG_ERROR = "dialog_error";
// Bool to track whether the app is already resolving an error
private boolean mResolvingError = false;
// etiqueta para logs
private final String TAG="Mapas";
//coordenadas-------------------
private String lat;
private String lon;
//variables para localizacion
Location mLastLocation;
Location mCurrentLocation;
LocationRequest mLocationRequest;
String mLastUpdateTime;
//// posicion anterior///////////////
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
// Create a GoogleApiClient instance
// Kick off the process of building a GoogleApiClient and requesting the LocationServices
// API.
buildGoogleApiClient();
//nuevo codigo
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
protected synchronized void buildGoogleApiClient() {
Log.v(TAG, "Lanzando GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
//location request
// actualiza cada 40 segundos min 20 segundos
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(40000);
mLocationRequest.setFastestInterval(20000);
//prioridad alta precision
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Log.v(TAG,"se ingresa al locationRequest");
}
@Override
protected void onResume() {
super.onResume();
// setUpMapIfNeeded();
}
@Override
protected void onPause(){
super.onPause();
Log.v(TAG, "evento onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.v(TAG, "evento onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.v(TAG, "evento onDestroy");
};
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia, and move the camera.
// habilitar controles de zoom
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//habilitar brujula
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.setMyLocationEnabled(true);
// para pasar a funcion
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object needed to retrieve the provider
Criteria criteria = new Criteria();
// Get the name of the best available provider
String provider = locationManager.getBestProvider(criteria, true);
// We can use the provider immediately to get the last known location
Location location = locationManager.getLastKnownLocation(provider);
googleMap.clear();
// convert the location object to a LatLng object that can be used by the map API
LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude());
Log.v(TAG, "Latitud :" + location.getLatitude());
googleMap.addMarker(new MarkerOptions().position(currentPosition).title("Yo"));
// zoom to the current location
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 13));
googleMap.setMyLocationEnabled(true);
Log.v(TAG, currentPosition.toString());
//pasar la posicion para grabar en el servidor
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
,這裏是我的觀點
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapaActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
你應該能夠獲得直通回調位置更新,onLocationChanged。看看這個鏈接的代碼,https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates – Sameer