Google Maps Android api v2 y ubicación actual

Estoy tratando de usar el nuevo google maps android api v2 y estoy desarrollando una aplicación para Android 2.3.3 o superior. Es una aplicación muy simple: toma la ubicación actual del usuario (usando gps o señal de redes) que obtiene de db un POI usando api dirección, que conduce al usuario al POI.

Mi problema es obtener la ubicación actual del usuario. Gracias a este mensaje ¿Cómo obtener la ubicación actual en Google Maps Android API v2? Me enteré de que no puedo actualizar la posición actual con el nuevo google api. Otro problema es que puedo establecer mi posición usando GoogleMap setMyLocationEnabled(boolean enabled) pero no puedo usar getMyLocation() para saber dónde está el usuario.

He utilizado esta guía http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation para obtener mi ubicación, y he tratado de integrar dentro de mi actividad para dibujar la posición del usuario.

Aquí mi código:

Manifiesto

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="it.mappe" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="it.mappe.permission.MAPS_RECEIVE" /> <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="MYGMAPSKEY" /> <activity android:name="it.mappe.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

actividad principal

 package it.mappe; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import android.content.Context; import android.content.Intent; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.provider.Settings; import android.support.v4.app.FragmentActivity; import android.widget.Toast; public class MainActivity extends FragmentActivity implements LocationListener { private GoogleMap map; private static final LatLng ROMA = new LatLng(42.093230818037,11.7971813678741); private LocationManager locationManager; private String provider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); boolean enabledGPS = service .isProviderEnabled(LocationManager.GPS_PROVIDER); boolean enabledWiFi = service .isProviderEnabled(LocationManager.NETWORK_PROVIDER); // Check if enabled and if not send user to the GSP settings // Better solution would be to display a dialog and suggesting to // go to the settings if (!enabledGPS) { Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show(); Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Define the criteria how to select the locatioin provider -> use // default Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); // Initialize the location fields if (location != null) { Toast.makeText(this, "Selected Provider " + provider, Toast.LENGTH_SHORT).show(); onLocationChanged(location); } else { //do something } } /* Request updates at startup */ @Override protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(provider, 400, 1, this); } /* Remove the locationlistener updates when Activity is paused */ @Override protected void onPause() { super.onPause(); locationManager.removeUpdates(this); } @Override public void onLocationChanged(Location location) { double lat = location.getLatitude(); double lng = location.getLongitude(); Toast.makeText(this, "Location " + lat+","+lng, Toast.LENGTH_LONG).show(); LatLng coordinate = new LatLng(lat, lng); Toast.makeText(this, "Location " + coordinate.latitude+","+coordinate.longitude, Toast.LENGTH_LONG).show(); Marker startPerc = map.addMarker(new MarkerOptions() .position(coordinate) .title("Start") .snippet("Inizio del percorso") .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))); } @Override public void onProviderDisabled(String provider) { Toast.makeText(this, "Enabled new provider " + provider, Toast.LENGTH_SHORT).show(); } @Override public void onProviderEnabled(String provider) { Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_SHORT).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } 

Disposición principal

 <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment" /> 

Funciona, pero no tan bueno, como se puede ver en las fotos eveytime hay una nueva ubicación que añade un nuevo Marcador (ex Overlay creo … Yo nunca uso viejo google maps api). Introduzca aquí la descripción de la imagen

¿Cómo puedo mostrar solo un marcador?

Además en mi mapa necesito exhibir también el marcador del POI, así que pienso que no es una buena solución delate todo el marcador y lo vuelva a dibujar en los mapas. Hay otra mejor manera de obtener la ubicación actual del usuario, la actualización de cada momento que muestra sólo un marcador personalizado ?!

Puede llamar a startPerc.remove(); Para eliminar sólo este marcador.

Desde aquí

¿Por qué no usar startPerc.setPosition para mover el marcador en lugar de seguir añadiendo y eliminando …

Establecer la posición del marcador cuando se obtiene otra ubicación por este método

 marker. set position (location) 

Usted debe utilizar googlemap.clear () antes de agregar el marcador porque cada vez cuando la localización cambió agrega el nuevo marcador en mapa con el marcador más viejo. Así que utilice googlemap.clear () antes de agregar el marcador en la ubicación cambiada. Se eliminará el marcador más antiguo y se añadirá un nuevo marcador. Googlemap es la instancia de GoogleMap Class.

Puede reemplazar el marcador por uno nuevo:

 if(userMarker!=null) userMarker.remove(); userMarker = theMap.addMarker(new MarkerOptions() .position(lastLatLng) .title("You are here") .icon(BitmapDescriptorFactory.fromResource(R.drawable.your_loc_icon)) .snippet("Your current location")); 

Atajo completo para definir el Google Map android v2 en android ……

Archivo XML:

 <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

Menifest archivo:

 <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- Map permission Starts --> <permission android:name="com.example.mapdemoapiv2.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.mapdemoapiv2.permission.MAPS_RECEIVE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <!-- Map permission Starts --> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.mapdemoapiv2.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MapDetail" > </activity> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="Your Key" /> <uses-library android:name="com.google.android.maps" /> </application> 

Clase de actividad:

 public class MainActivity extends android.support.v4.app.FragmentActivity { GoogleMap googleMap; MarkerOptions markerOptions; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUpMapIfNeeded(); GetCurrentLocation(); } private void setUpMapIfNeeded() { if (googleMap == null) { Log.e("", "Into null map"); googleMap = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter( MainActivity.this)); if (googleMap != null) { Log.e("", "Into full map"); googleMap.setMapType(googleMap.MAP_TYPE_NORMAL); googleMap.getUiSettings().setZoomControlsEnabled(false); } } } private void GetCurrentLocation() { double[] d = getlocation(); Share.lat = d[0]; Share.lng = d[1]; googleMap .addMarker(new MarkerOptions() .position(new LatLng(Share.lat, Share.lng)) .title("Current Location") .icon(BitmapDescriptorFactory .fromResource(R.drawable.dot_blue))); googleMap .animateCamera(CameraUpdateFactory.newLatLngZoom( new LatLng(Share.lat, Share.lng), 5)); } public double[] getlocation() { LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); List<String> providers = lm.getProviders(true); Location l = null; for (int i = 0; i < providers.size(); i++) { l = lm.getLastKnownLocation(providers.get(i)); if (l != null) break; } double[] gps = new double[2]; if (l != null) { gps[0] = l.getLatitude(); gps[1] = l.getLongitude(); } return gps; } 

En primer lugar en el mapa api v2 use LocationClient para recuperar su ubicación, consulte: http://developer.android.com/training/location/retrieve-current.html

Iportant: para poner en marcha la API, llame a esto en onStart of main Activity:

 private void kickStardedLocationManager(){ // Acquire a reference to the system Location Manager LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates android.location.LocationListener locationListener = new android.location.LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. //MainActivity.this.makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; long l = 10; float f = 100; // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); //LocationManager.GPS_PROVIDER } 
  • Serialización de Java - deserialización de Android
  • ¿Por qué mi editText no presenta la salida correcta en android?
  • ¿Cómo iniciar la aplicación de Android desde la URL en android?
  • Conversión de cadena-fecha con nanosegundos
  • Exportación y ejecución del proyecto Unity3D a Android Studio
  • Cómo inicializar el contenido de la pestaña cuando se inicializa la aplicación
  • Agregar elemento a arraylist si aún no existe en la lista
  • ¿Cómo se hacen los widgets personalizados?
  • Bloquear una llamada de red desde una aplicación Android de SDK
  • Pestaña Switch onNewIntent causa IllegalStateException
  • Android: búsqueda del botón por id
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.