Get current android location position

Location.java

package com.localisation; import java.io.IOException; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.TextView; import android.widget.Toast; public class localisation extends Activity implements OnClickListener, LocationListener{ private LocationManager lManager; private Location location; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //On spécifie que l'on va avoir besoin de gérer l'affichage du cercle de chargement requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.main); //On récupère le service de localisation lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //On affecte un écouteur d'évènement aux boutons findViewById(R.id.obtenir_position).setOnClickListener(this); } //Méthode déclencher au clique sur un bouton public void onClick(View v) { obtenirPosition(); } } private void afficherLocation() { //On affiche les informations de la position a l'écran ((TextView)findViewById(R.id.latitude)).setText(String.valueOf(location.getLatitude())); ((TextView)findViewById(R.id.longitude)).setText(String.valueOf(location.getLongitude())); ((TextView)findViewById(R.id.altitude)).setText(String.valueOf(location.getAltitude())); } public void onProviderDisabled(String provider) { //Lorsque la source (GSP ou réseau GSM) est désactivé Log.i("Tuto géolocalisation", "La source a été désactivé"); //...on affiche un Toast pour le signaler à l'utilisateur Toast.makeText(localisation.this, String.format("La source \"%s\" a été désactivé", provider), Toast.LENGTH_SHORT).show(); //... et on spécifie au service que l'on ne souhaite plus avoir de mise à jour lManager.removeUpdates(this); //... on stop le cercle de chargement setProgressBarIndeterminateVisibility(false); } public void onProviderEnabled(String provider) { Log.i("Tuto géolocalisation", "La source a été activé."); } public void onStatusChanged(String provider, int status, Bundle extras) { Log.i("Tuto géolocalisation", "Le statut de la source a changé."); } } 

main.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="Latitude" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_marginLeft="10dip" /> <EditText android:text="0.0" android:id="@+id/latitude" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:editable="false" android:focusable="false" android:gravity="center" android:layout_marginRight="10dip" android:layout_width="150dip" /> <TextView android:text="Longitude" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/longitude" android:layout_alignTop="@+id/longitude" android:gravity="center" android:layout_marginLeft="10dip" /> <EditText android:text="0.0" android:id="@+id/longitude" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:editable="false" android:focusable="false" android:gravity="center" android:layout_marginRight="10dip" android:layout_width="150dip" android:layout_below="@+id/latitude" /> <TextView android:text="Altitude" android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/altitude" android:layout_alignTop="@+id/altitude" android:gravity="center" android:layout_marginLeft="10dip" /> <EditText android:text="altitude" android:id="@+id/altitude" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:editable="false" android:focusable="false" android:gravity="center" android:layout_marginRight="10dip" android:layout_below="@+id/TextView02" android:layout_width="150dip" /> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/TextView03" android:layout_centerHorizontal="true" android:layout_marginTop="10dip"> <Button android:layout_height="wrap_content" android:id="@+id/obtenir_position" android:text="Obtenir Position" android:layout_width="100dip" /> </LinearLayout> </RelativeLayout> 

manifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.localisation" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".localisation" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </application> </manifest> 

I have a problem in these codes. My goal is to determine my position (longitude, latitude and altitude.

I have certain places in the database (longitude, latitude and height, and I want to determine the closest place.) (With google Map API (without map display)

Can you help me and explain to me in detail what I am doing now?

Thank you very much

+4
source share
3 answers

I use this code to get the Latitude and longitude of the current location:

 LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener); Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); double latitude=0; double longitude=0; latitude = location.getLatitude(); longitude = location.getLongitude(); 
+11
source

I'm not sure if you ask at all: "How do I get the gps location?" or more specific "what's wrong with this code."

If its common - it appears in How to get the current GPS location programmatically in Android? . In addition, I suggest you be careful with the getLastKnownLocation method. The last place can be terribly outdated. I also suggest you start a gps survey before you really need it, sometimes it may take a few seconds (or more) to get your first gps read.

If its specific - perhaps explain what problems you have?

0
source

we’ll create an Android app that displays the current location on Google Maps using the Google Maps Android V2 API.

Since the Google map is displayed using SupportMapFragment, this application can run in Android API 8 or higher. To use MapFragment for Google Maps, we need an Android device with API level 12 or higher.

This application was developed in Eclipse 4.2.1 with the ADT plugin (21.0.0) and the Android SDK (21.0.0). This application has been tested on a real Android phone with Android (2.3.6). An alternative method for this application is available when showing the current location using OnMyLocationChangeListener in Google Map Android API V2

Download code

0
source

Source: https://habr.com/ru/post/1343799/


All Articles