在Android中捕获GPS坐标

在Android中捕获GPS坐标,android,gps,Android,Gps,我有这段代码(大部分来自教程),它以十进制形式显示GPS坐标。我想用这个按钮把当前位置保存到一些数据结构中,或者什么都不保存。。。。那部分不是问题所在。我想知道如何最好地从大量更新中获取GPS数据 我想捕获onclick侦听器中的坐标 我应该使用getLasKnown()吗 package com.example.geolocation; 导入android.app.Activity; 导入android.content.Context; 导入android.location.location;

我有这段代码(大部分来自教程),它以十进制形式显示GPS坐标。我想用这个按钮把当前位置保存到一些数据结构中,或者什么都不保存。。。。那部分不是问题所在。我想知道如何最好地从大量更新中获取GPS数据

我想捕获onclick侦听器中的坐标

我应该使用getLasKnown()吗

package com.example.geolocation;
导入android.app.Activity;
导入android.content.Context;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类MainActivity扩展了活动{
TextView textLat,textLong;
按钮位置;
双mLat,mLong;
ArrayList mLats=新的ArrayList();
ArrayList mLongs=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLat=(TextView)findViewById(R.id.lat_输出);
textLong=(TextView)findViewById(R.id.long\u输出);
locateBTN=(按钮)findViewById(R.id.locate_按钮);
位置管理器位置管理器=
(LocationManager)getSystemService(Context.LOCATION\u服务);
位置侦听器位置侦听器=
新建myLocationlistener();
位置经理
.RequestLocationUpdate(LocationManager.GPS\u提供程序,
0,0,locationListener);
locateBTN.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//存根
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
私有类myLocationlistener实现LocationListener{
@凌驾
已更改位置上的公共无效(位置){
如果(位置!=null){
double-pLong=location.getLongitude();
双平台=location.getLatitude();
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
}
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
}
}

单击按钮,您可以使用LocationManager对象GetLastKnown定位方法-

获取GPS坐标的最佳方法是使用
谷歌的融合定位API


否则,你就得加上很多代码才能得到正确的坐标。与仅使用GPS坐标相比,此Api消耗的电池也少得多

您希望何时捕获坐标?按按钮?还是每x分钟一次?用户何时移动?etcon按钮按下。我确信onClickListener中应该包含哪些代码。。。
package com.example.geolocation;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;


public class MainActivity extends Activity {

TextView textLat, textLong;
Button locateBTN;

double mLat, mLong;

ArrayList<Double> mLats = new ArrayList<Double>();
ArrayList<Double> mLongs = new ArrayList<Double>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textLat = (TextView) findViewById(R.id.lat_output);
    textLong = (TextView) findViewById(R.id.long_output);

    locateBTN = (Button) findViewById(R.id.locate_button);

    LocationManager locationManager =
            (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener =
            new myLocationlistener();
    locationManager
            .requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    0, 0, locationListener);

    locateBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private class myLocationlistener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if ( location != null ){
            double pLong = location.getLongitude();
            double pLat = location.getLatitude();

            textLat.setText(Double.toString(pLat));
            textLong.setText(Double.toString(pLong));

        }
    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}
}