我想显示纬度和经度我当前的位置.. 为此而不是在谷歌搜索,我搜索了SO。
我只是想显示我当前的位置纬度和经度。
请参阅下面的代码:
public class LocationGetter extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new LocationManagerHelper();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
+ '\n');
tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
+ '\n');
Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));
} else {
tv.setText("GPS is not turned on...");
}
/** set the content view to the TextView */
setContentView(tv);
}
/* Class My Location Listener */
public static class LocationManagerHelper implements LocationListener {
private static double latitude;
private static double longitude;
@Override
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public static double getLatitude() {
return latitude;
}
public static double getLongitude() {
return longitude;
}
}
}
我还在清单文件中添加了权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
查看我得到的输出:
我哪里错了?我不明白,它是一个简单的代码,为什么它不起作用?
答案 0 :(得分:2)
我刚刚验证了您的代码并且它可以使用这些更改,您只需要将tv.append()
调用移动到每次调用的onLocationChanged()
方法,如果您不使用它CallBack,那么你将只得到第一个设定值,它只执行一次。
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
+ '\n');
tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
+ '\n');
Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));
}
我在AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
您可以查看这些内容并过滤掉您不感兴趣的内容。我使用了here下载的.gpx文件
我已经在Android 2.2上测试了它,
答案 1 :(得分:2)
你在Onlocationchanged中有位置
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
}
因此,仅当从当前位置进行更改时才会显示纬度和经度。用于检查您是否给出了经度和经度并从DDMS中的模拟器控件发送,然后运行您的程序。它将显示位置
答案 2 :(得分:1)
如果您在设备中运行此程序,则它会显示纬度和经度0.0,直到我们更改位置。您必须从当前位置移动至少10米才能看到输出。因为没有更改当前位置onLocationChange()
方法不会触发,输出将为0.0
答案 3 :(得分:0)
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
对getLastKnownLocation()
的调用不会阻止 - 这意味着如果当前没有可用的位置,它将返回null
- 因此您可能希望查看将LocationListener
传递给相反,requestLocationUpdates()
method会为您提供位置的异步更新。
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener);
如果您想使用GPS,则需要为您的应用程序提供ACCESS_FINE_LOCATION
permission。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
您可能还想在GPS不可用时添加ACCESS_COARSE_LOCATION
permission,并使用getBestProvider()
method选择您的位置提供商。
答案 4 :(得分:0)
我认为使用模拟器无法获得GPS坐标。如果您正在使用移动设备,请在建筑物外部尝试GPS。有时你不会在建筑物内获得GPS坐标。如果那个时候你也失败了,那么我将发布我们成功的代码。
以下代码我刚刚测试了它的作品..
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 GetGPS extends Activity {
String GPSPROVIDER = LocationManager.GPS_PROVIDER;
private static final long MIN_GEOGRAPHIC_POOLING_TIME_PERIOD = 10000;
private static final float MIN_GEOGRAPHIC_POOLING_DISTANCE = (float)5.0;
public LocationManager gpsLocationManager;
static Context context = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
/*Get a listener for GPS*/
LocationListener gpsLocationListener = null;
gpsLocationListener = new GpsLocationListener(this);
/*Start Location Service for GPS*/
gpsLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
/*Register GPS listener with Location Manager*/
gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_GEOGRAPHIC_POOLING_TIME_PERIOD,
MIN_GEOGRAPHIC_POOLING_DISTANCE, gpsLocationListener);
boolean isavailable = gpsLocationManager.isProviderEnabled(GPSPROVIDER);
if(isavailable) {
Location loc = gpsLocationManager.getLastKnownLocation("gps");
if(loc != null) {
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
Toast.makeText(GetGPS.this,"Longitude is "+longitude + " Latitude is "+latitude, Toast.LENGTH_LONG).show();
}
}
}
public static class GpsLocationListener implements LocationListener {
public GpsLocationListener(Context context){
}
public void onLocationChanged(Location loc) {
if (loc != null) {
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
Toast.makeText(context,"Longitude is "+longitude + " Latitude is "+latitude, Toast.LENGTH_LONG).show();
}
}//End of onLocationChanged Method
@Override
public void onProviderDisabled(String provider) {
}//End of onProviderDisabled Method
@Override
public void onProviderEnabled(String provider) {
}//End of onProviderEnabled Method
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}//End of onStatusChanged Method
}//End of GpsLocationListener class
}
我通过将设备从一个地方移动到另一个地方来测试这个......
我希望它会有所帮助..