如何获得Android的GPS位置
干杯,我想通过Android获取当前的GPS位置。我也跟着this Tutorial和Vogellas article。虽然它不起作用。使用LocationManager.NETWORK_PROVIDER时,无论我站在何处,我总是可以获得51的经度和9的经度。当使用LocationManager.GPS_PROVIDER时,我一无所获。如何获得Android的GPS位置
尽管使用GMaps时一切正常:S不知道为什么。我怎样才能获得像GMaps一样的当前GPS位置?
这里是我的代码:
package com.example.gpslocation;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
public class GPS_Location extends Activity implements LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps__location);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gps__location, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
int latitude = (int) (location.getLatitude());
int longitude = (int) (location.getLongitude());
Log.i("Geo_Location", "Latitude: " + latitude + ", Longitude: " + longitude);
}
@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
}
}
这是你的问题:
int latitude = (int) (location.getLatitude());
int longitude = (int) (location.getLongitude());
纬度和经度double
- 值,因为它们代表了位置以度为单位。
通过将它们投射到int
,您将丢弃逗号后面的所有内容,这会产生很大的差异。见"Decimal Degrees - Wiki"
试试看:
public LatLng getLocation()
{
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
Double lat,lon;
try {
lat = location.getLatitude();
lon = location.getLongitude();
return new LatLng(lat, lon);
}
catch (NullPointerException e){
e.printStackTrace();
return null;
}
}
感谢您的答案,但我已经尝试了上面的代码,只是切割我自己的代码以最简单的解决方案向您展示问题; P谢谢 – JustBasti 2013-05-11 15:09:00
这段代码有一个问题:
int latitude = (int) (location.getLatitude());
int longitude = (int) (location.getLongitude());
您可以更改int
到double
double latitude = location.getLatitude();
double longitude = location.getLongitude();
最初的问题是通过改变lat
和lon
翻番解决。
我想添加评论到解决方案Location location = locationManager.getLastKnownLocation(bestProvider);
它的工作原理是找出最后一个已知的位置,当其他应用程序正在lisnerning。例如,如果自设备启动后没有应用程序执行此操作,代码将返回零(我最近花了一些时间来弄清楚)。
而且,这是一个很好的做法,停止监听时,有没有必要通过locationManager.removeUpdates(this);
而且,即使有权限manifest
,当位置服务在设备上的Android设置中启用代码工作。
摘录: -
try
{
cnt++;scnt++;now=System.currentTimeMillis();r=rand.nextInt(6);r++;
loc=lm.getLastKnownLocation(best);
if(loc!=null){lat=loc.getLatitude();lng=loc.getLongitude();}
Thread.sleep(100);
handler.sendMessage(handler.obtainMessage());
}
catch (InterruptedException e)
{
Toast.makeText(this, "Error="+e.toString(), Toast.LENGTH_LONG).show();
}
正如你可以在上面看到,一个线程运行旁边的用户界面活动的主线,其连续显示GPS纬度,经度非常久远的当前时间和随机骰子掷。
如果你很好奇,然后只检查全码: GPS Location with a randomized dice throw & current time in separate thread
对于更高级别,更容易的方式来获得GPS定位,你可以使用这个库:https://github.com/delight-im/Android-SimpleLocation – caw 2015-03-04 22:19:20
检查http:// stackoverflow。问题/ 1513485 /如何获得当前的GPS位置编程在Android/38897436#38897436 – 2016-08-11 13:13:59