简体   繁体   English

获取当前位置android

[英]get current position location android

Location.java 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 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 的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 those codes. 我在这些代码中有问题。 My goal is determine my position (longitude, latitude and altitude. 我的目标是确定我的位置(经度,纬度和海拔高度。

In a database i have some places defined by (longitude, latitude and altitude and i want to determine the most near place. (with google Map API (without displaying the map) 在数据库中我有一些地方定义(经度,纬度和海拔高度,我想确定最近的地方。(使用谷歌地图API(不显示地图)

Can you help me please and explain me in details what i do now? 你能帮帮我,详细解释一下我现在做的事吗?

Thank you very much 非常感谢你

I am using this code to get the Latitude and longitude of 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();

we will create an Android application which will display current location in Google Maps using Google Maps Android API V2. 我们将创建一个Android应用程序,该应用程序将使用Google Maps Android API V2在Google地图中显示当前位置。

Since the Google Map is displayed using SupportMapFragment, this application can run in Android API Level 8 or above. 由于使用SupportMapFragment显示Google Map,因此该应用程序可以在Android API Level 8或更高版本中运行。 In order to use MapFragment for Google Maps, we need Android device with API level 12 or above. 要将MapFragment用于Google地图,我们需要API级别为12或以上的Android设备。

This application is developed in Eclipse 4.2.1 with ADT plugin ( 21.0.0 ) and Android SDK ( 21.0.0 ). 此应用程序是在Eclipse 4.2.1中使用ADT插件(21.0.0)和Android SDK(21.0.0)开发的。 This application is tested in a real Android Phone with Android ( 2.3.6 ). 此应用程序在Android(2.3.6)的真实Android手机中进行了测试。 An alternative method for this application is available at Showing current location using OnMyLocationChangeListener in Google Map Android API V2 在Google Map Android API V2中使用OnMyLocationChangeListener显示当前位置可以使用此应用程序的替代方法

Download code 下载代码

I'm not sure if you are asking in general, "how do I get the gps location?" 我不确定你是否一般会问,“我如何获得GPS位置?” or more specific "what's wrong with this code". 或更具体的“这段代码有什么问题”。

If its general- this appears in How do I get the current GPS location programmatically in Android? 如果它的一般 - 这将出现在如何在Android中以编程方式获取当前GPS位置? . Also, I suggest you be careful with the getLastKnownLocation method. 另外,我建议你小心使用getLastKnownLocation方法。 The last location can be horribly out of date. 最后一个位置可能会过时。 I also suggest you start polling gps before you actually need it, it can sometimes take a few seconds (or more) to get your first gps reading. 我还建议您在实际需要之前开始轮询gps,有时需要几秒钟(或更长时间)才能获得第一个gps读数。

If its specific- maybe explain what problems you're having? 如果具体 - 也许可以解释一下你遇到了什么问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM