android获取GPS位置

标签 android gps android-edittext

我试图通过按下按钮将我的 GPS 位置放入编辑框

当我单击按钮时,GPS 图标会亮起并开始闪烁,但直到稳定后才会用位置填充编辑框,所以我必须按下按钮,等到 GPS 图标稳定后再按下按钮再次将位置放入编辑框中

有没有一种方法可以通过按下一个按钮来完成,这样它就会等到有 GPS 信号,然后才用位置填充编辑框

我的代码如下

public void pickbtn(View view) {

         LocationManager mlocManager=null;
         LocationListener mlocListener;
         mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
         mlocListener = new locapp();
         mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

         if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if(locapp.latitude>0)
            {
                Calendar c = Calendar.getInstance(); 
                int dd = c.get(Calendar.SHORT);
                Time t = new Time(Time.getCurrentTimezone());
                t.setToNow();
                String date = t.format("%d/%m/%y");

                String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
                EditText editText1 = (EditText)findViewById(R.id.LatText);
                editText1.setText(mydate + ","+locapp.latitude + "," +locapp.longitude, TextView.BufferType.EDITABLE);
             }
             else
             {
              }
          } else {
          }
      }

locapp.java代码如下

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
public class locapp implements LocationListener {
public static double latitude;
public static double longitude;
@Override
public void onLocationChanged(Location loc)
{
    loc.getLatitude();
    loc.getLongitude();
    latitude=loc.getLatitude();
    longitude=loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider)
{
   //print "Currently GPS is Disabled";
}
@Override
public void onProviderEnabled(String provider)
{
    //print "GPS got Enabled";
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}

感谢任何帮助

标记

最佳答案

基于 Will Jamieson 的回答。我正在添加一个类以从 GPS 获取位置。在您的项目中添加此 GPSTracker 类:http://pastie.org/9300205

将其全局化:

GPSTracker gps;

在 onCreate 的 Activity 中使用此代码:

gps = new GPSTracker(this);

在你的类中定义这个异步:

private class getGPSstuff extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    Location l=gps.getLocation();
    latitude=l.getLatitude();
    longitude=l.getLongitude();
}

@Override
protected void onPostExecute(String result) {
    // Set the text here
}

@Override
protected void onPreExecute() {
 // you can make sure gps is available here and handle it if its not available here
 // optional tho
}
}

在按钮点击时使用它:

new getGpsStuff().execute();

关于android获取GPS位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24272683/

相关文章:

android - 自定义字体字符分开?

android - 无法在选项卡中获得不同的 View

java - 如何获取当前位置和下一个当前位置之间的距离

java - 在处理层映射 GPS 点

Android - onTextChanged() 在手机方向改变时调用

android - EditText 选择器的名称是什么,如何更改它? (含图片)

android-edittext - 在Android中动态创建EditText

java - 删除广告时调整布局大小

android - 位图资源取决于屏幕密度

java - 需要帮助纠正 Java 中 GT06 GPS 设备的登录响应