2014-10-29 66 views
0

我有这个代码(大部分来自教程),它以十进制形式显示GPS坐标。我想用按钮将当前位置保存在某个数据结构中,或者不是......那部分不是问题。我想知道如何从似乎是大量更新中获得最佳GPS数据。在Android中捕获GPS坐标

我想捕获onClickListener中的坐标。

我应该使用getLasKnown()吗?

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) { 

    } 
} 
} 
+0

什么时候你想捕捉坐标?在按钮上按?或者每隔x分钟?当用户移动?等 – zafrani 2014-10-29 02:15:25

+0

按钮按下。我相信什么代码应该在onClickListener中去... – StillLearningToCode 2014-10-29 02:47:24

回答