使用GPS坐标的帮助,Android版 [英] help in using GPS coordinates, Android
本文介绍了使用GPS坐标的帮助,Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用这code,让我的位置,并在屏幕上的坐标。
I am using this code to get my location and print the coordinates on the screen.
package com.example.alpha;
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.widget.Toast;
public class alpha extends Activity
{
private LocationManager lm;
private LocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---use the LocationManager class to obtain GPS locations---
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
double lat = loc.getLatitude();
double lon = loc.getLongitude();
Toast.makeText(getBaseContext(), "Location changed : alphaLat: " + lat +
" alphaLng: " + lon,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
}
问题是,我想使用的纬度和经度值,以便通过流发送。如何获得onLocationChanged方法之外的坐标有什么建议?
The problem is that i want to use the lat and lon values, in order to send them via a stream. Any advice on how to get the coordinates outside the onLocationChanged method?
推荐答案
而不是声明纬度和经度为局部变量,声明为类级变量。这样,你可以在外面onLocationChanged方法访问这些值。
Instead of declaring lat and lon as local variables, declare them as Class level variables. That way you can access these values outside onLocationChanged method.
public class alpha extends Activity
{
private LocationManager lm;
private LocationListener locationListener;
double lat;
double lon;
public class MyLocationListener implements LocationListener
{
...
lat = loc.getLatitude();
lon = loc.getLongitude();
........
这篇关于使用GPS坐标的帮助,Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文