gps位置值不打印android

时间:2013-09-12 10:22:11

标签: android gps textview

在这段代码中我想得到经度和纬度并将它们转换为字符串 在屏幕上使用它 和短信 那我该怎么处理呢 我应该上新课吗?或者只是修改了这段代码

我该怎么做才能让它发挥作用 我使用了所有的位置

public class MainActivity extends Activity implements LocationListener  {

    Location location; // location

    LocationManager locationManager;
    public String LatTxt;
    public String LonTxt;
    public Double latitude; // latitude
    public Double longitude; // longitude
    TextView latTextView = (TextView) findViewById(R.id.Latitude_Txt);
    TextView lonTextView = (TextView) findViewById(R.id.Longitude_Txt);
    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = (long) (1000 * 60 * 0.25); // 0.25
                                                                                // minute

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

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,locListener);

        lonTextView.setText(" " + location.getLongitude());
        latTextView.setText(" " + location.getLatitude());

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        latitude = location.getLatitude();
        LatTxt = latitude.toString();
        longitude = location.getLongitude();
        LonTxt = longitude.toString();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}






<TextView
                    android:id="@+id/Latitude_Txt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white"
                    android:gravity="center_horizontal"
                    android:text="Latitude"
                    android:textColor="@android:color/black"
                    android:textSize="16sp" />
            </LinearLayout>

2 个答案:

答案 0 :(得分:0)

lonTextView.setText(" " + location.getLongitude());
latTextView.setText(" " + location.getLatitude());

@Override
public void onLocationChanged(Location location) {


}

您尝试在location中打印protected void onCreate(Bundle savedInstanceState),但location未启动。

BTW,在行之前添加:

 lonTextView.setText(" " + location.getLongitude());
 latTextView.setText(" " + location.getLatitude());

此代码,初始化旧位置

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,this); 
Criteria criteria = new Criteria();

locationManager.getLastKnownLocation(provider);
String provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);

并删除您的requestLocationUpdates

<强> [编辑]

 public class MainActivity extends Activity implements LocationListener  {

Location location; // location

LocationManager locationManager;
public String LatTxt;
public String LonTxt;
public Double latitude; // latitude
public Double longitude; // longitude
TextView latTextView = (TextView) findViewById(R.id.Latitude_Txt);
TextView lonTextView = (TextView) findViewById(R.id.Longitude_Txt);
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = (long) (1000 * 60 * 0.25); // 0.25
                                                                            // minute

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

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener locListener = this;
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,locListener);



}

@Override
public void onLocationChanged(Location location) {

    // TODO Auto-generated method stub
    latitude = location.getLatitude();
    LatTxt = latitude.toString();
    longitude = location.getLongitude();
    LonTxt = longitude.toString();

    lonTextView.setText(" " + location.getLongitude());
    latTextView.setText(" " + location.getLatitude());
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

 }
}

答案 1 :(得分:0)

收到新的GPS坐标时。将调用onLocationChanged函数。你应该在那里进行GPS坐标操作