好的密码破译,这里是我如何实现我的位置提取程序:
// the listener to listen to the locations
private LocationListener listener = null;
// a location manager
private LocationManager lm = null;
// locations instances to GPS and NETWORk
private Location myLocationGPS, myLocationNetwork;
// instantiates fields
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationNetwork = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
myLocationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
listener = new myLocationListener();
// the listener that gonna notify the activity about location changes
public class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// "location" is the RECEIVED locations and its here that you should proccess it
// check if the incoming position has been received from GPS or network
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
lm.removeUpdates(this);
} else {
lm.removeUpdates(listener);
}
}
@Override
public void onProviderDisabled(String provider) {
lm.removeUpdates(this);
lm.removeUpdates(listener);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
正如你所看到的,有没有这么很多技巧。在您实例化侦听器时,您应该会看到Android设备顶部的GPS图标。此外,只要您的位置发生变化(即随着您的设备走动),OnLocationChanged方法将被调用。
另外,有趣的是,如果你只想得到你的位置,有几种方法可以做到这一点,然后以不同的回报速度和不同的疾病。请检查GoogleGLM(请求http://www.google.com/glm/mmap,返回json编码与您的位置一致)服务,三角测量和网络位置。在上面的片段中,我展示了如何通过GPS和网络获取位置。希望它有一些帮助... :)
我没有得到你的问题......你想通过GPS获取用户的当前位置?如果是,那么它的“非常简单”:使一个类从LocationListener扩展并覆盖onLocationChanged方法。您是否已将您的MD5 API密钥用于Google地图? – mthama
@mthama是和是..:D Infact这里是我的[原问题](http://stackoverflow.com/questions/14692202/unable-to-get-current-gps-location-co-ordinates)。我已经完成了演习:D keystore..console..api key.pftt:p我缺少某处某处..或很可能..my电话被窃听:但是,在这个世界怎么样?我的手机的谷歌地图应用程序可以锁定到GPS卫星上吗? :哦,顺便说一句,你是否尝试过在HTC渴望上获得GPS锁?或者在基于android 2.3.3的设备上播放它? :| (实时..没有DDMS thingy) – beerBear
你的代码是复杂的,看看有什么不正确的。我只能说你可以使用Criteria,也可以在没有它的情况下请求GPS_PROVIDER。而“getMyLocation”是不好的类名。 Java中的类名应以大写字母开头,且不能是动词。getMyLocation是一个很好的方法名,但是类应该被命名为LocationRetreiver或者其他东西:)并且记住要获得一个你可能希望在外面的GPS解析修复,或者更好地使用FakeGPS https://play.google.com/store/ apps/details?id = com.lexa.fakegps - 如果一切正常,你应该马上得到定位。 –