单击按钮后如何在不同位置获取GPS坐标

时间:2012-03-02 03:55:11

标签: android gps provider location-provider

我想使用GPS_PROVIDER点击按钮找到当前位置的GPS坐标。点击“A”按钮后,我将关闭应用程序并转到地点B并再次单击按钮以获取B坐标。我怎样才能做到这一点 ?我是否必须定义响应位置更新的侦听器?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取gps数据

package com.gpsexample.location;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public class LocListener implements LocationListener
{
    private static double lat =0.0;
    private static double lon = 0.0;
    private static double alt = 0.0; 
    private static double speed = 0.0;

    public static double getLat()
    {
        return lat;
    }

    public static double getLon() 
    {
        return lon;
    }

    public static double getAlt()
    {
        return alt;
    }

    public static double getSpeed()
    {
        return speed;
    }

    @Override
    public void onLocationChanged(Location location) 
    {
        lat = location.getLatitude();
        lon = location.getLongitude();
        alt = location.getAltitude();
        speed = location.getSpeed(); 
    }

    @Override
    public void onProviderDisabled(String provider) {}
    @Override
    public void onProviderEnabled(String provider) {}
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}