Google Maps api Android v2 et de l'emplacement actuel

Je suis en train d'utiliser la nouvelle version de google maps api android v2 et je suis le développement d'une application pour android 2.3.3 ou supérieur.
Il est très simple d'application:
il prend l'emplacement actuel de l'utilisateur (en utilisant le gps ou les réseaux de signal)
il obtient à partir de la db a POI
à l'aide de la direction de l'api, il pousse l'utilisateur à la PI.

Mon problème est d'obtenir l'emplacement actuel de l'utilisateur. merci pour ce post Comment faire pour obtenir l'emplacement actuel dans Google Maps API Android v2? j'ai appris que je ne peux pas mettre à jour position actuelle en utilisant la nouvelle api de google.
Un autre problème est que je peux mettre ma position à l'aide de GoogleMap setMyLocationEnabled(boolean enabled) mais je ne peux pas utiliser getMyLocation() de savoir où l'utilisateur est.

J'ai utilisé ce guide http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation pour obtenir ma position, et j'ai essayé de l'intégrer à l'intérieur de mon Activité de tirage de position utilisateur.

voici mon code:

Manifeste

<?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>

Activité principale

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
}
}

en page principale

<?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" />

Il fonctionne, mais pas si bon, comme vous pouvez le voir sur les photos eveytime il y a un nouvel emplacement, il ajoute un nouveau Marqueur (ex Superposition je pense... je n'ai jamais utiliser de vieux api google maps).
Google Maps api Android v2 et de l'emplacement actuel

Comment puis-je montrer une seule de marqueurs?

En plus sur ma carte j'ai besoin d'afficher également des POI marqueur, donc je pense que ce n'est pas une bonne solution de supprimer tous les marqueurs et les redessiner sur les cartes.
Il y a une autre meilleure façon d'obtenir de l'utilisateur emplacement actuel, la mise à jour à chaque instant en montrant un marqueur personnalisé?!

InformationsquelleAutor antedesk | 2012-12-12