android + google map api v2 + emplacement actuel
je suis entrain de créer une application avec une carte google map vue et je veux cette application pour détecter l'emplacement actuel de l'utilisateur et l'affichage de la lat et long avec un marqueur sur la carte, mais tout ce que je peux faire jusqu'à maintenant, c'est l'affichage de la carte google et la détection de l'emplacement actuel, sans marqueur affiché et sans aucun texte apparaissent à l'affichage de la lat et long
je n'ai pas ajouter un marqueur jusqu'à maintenant parce que je ne sais pas où le mettre dans le code et quelle est la meilleure pratique pour faire
quelqu'un peut m'aider ???
c'est le code de l':
fichier manifeste
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.locationgooglemapv2demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="in.wptrafficanalyzer.LocationGoogleMapV2Demo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="in.wptrafficanalyzer.LocationGoogleMapV2Demo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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" >
<activity
android:name="in.wptrafficanalyzer.locationgooglemapv2demo.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API KEY" />
</application>
</manifest>
fichier xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
fichier java
package in.wptrafficanalyzer.locationgooglemapv2demo;
import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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.LatLng;
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
//Showing status
if(status!=ConnectionResult.SUCCESS){ //Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { //Google Play Services are available
//Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
//Getting GoogleMap object from the fragment
googleMap = fm.getMap();
//Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
//Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
//Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
//Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
//Getting latitude of the current location
double latitude = location.getLatitude();
//Getting longitude of the current location
double longitude = location.getLongitude();
//Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
//Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
//Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
}
@Override
public void onProviderDisabled(String provider) {
//TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
//TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
//TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
source d'informationauteur user2277081
Vous devez vous connecter pour publier un commentaire.
D'abord vérifier la documentation iciil donne une bonne indication sur les meilleures stratégies pour faire de position utilisateur.
Personnellement j'ai l'habitude de démarrer la carte avec un marqueur placé à la position retrivied avec
et puis j'ai commencer à l'écoute pour la mise à jour de position et de déplacer le marqueur.
Il dépendra aussi de la façon dont la position exacte dont vous avez besoin pour la pourpose de votre application, si vous n'avez pas besoin de vraiment précis de la position, mais la zone est suffisant peut-être vous pouvez simplement utiliser le
lastKnownLocation
.Vous pouvez essayer quelque chose comme cela
Aussi, vous n'avez pas besoin de la classe d'Activité à
implements LocationListener
il vous suffit de définir un écouteur, et de l'inscrire comme je l'ai fait ci-dessusP. S. je n'ai pas un éditeur fils mon code peut contenir des fautes de frappe
Espère que cette aide, les acclamations
J'ai juste eu le même problème, et trouvé un moyen de le faire en utilisant l'API Google Maps:
et puis définissez l'écoute de la carte:
Ce sera appelée lorsque la carte trouve d'abord l'emplacement.
Pas besoin de
LocationService
ouLocationManager
à tous.Plein de raccourci pour la définition de la Carte de Google, android v2 android......
Fichier XML :
Menifest fichier :
Classe d'activité :
Il est préférable d'utiliser:
La Documentation
facile comme ça:
après ce -
mettre ce -