[英]Android GPS app not working
i have tryed to make my own app and to use google maps. 我曾尝试制作自己的应用程序并使用Google地图。 I want it to set the center of the map on my curent gps position, but when i have a gps lock on my phone i will just go to these coordinates (0,0) I dont know where i went wrong. 我希望它在当前gps位置上设置地图的中心,但是当我在手机上使用gps锁定时,我只会转到这些坐标(0,0),我不知道我哪里出错了。 Thanks everybody :D 谢谢大家:D
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class Courses extends MapActivity {
MapView map;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.courses);
MapView map = (MapView) findViewById (R.id.MapView);
map.setBuiltInZoomControls(true);
map.setSatellite(true);
final MapController control = map.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
control.setCenter(new GeoPoint((int)location.getLatitude(),(int)location.getLongitude()));
control.setZoom(19);
}
@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
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
} ` }`
The first issue I see here is, that location.getLatitude() and location.getLongitude() return float , which have to be multiplied with 1E6 and then casted as an int to be acceptible for GeoPoint . 我在这里看到的第一个问题是, location.getLatitude()和location.getLongitude()返回float ,必须将它们乘以1E6,然后将其强制转换为可以接受GeoPoint的整数 。 This also explains why you have coordinates of approximately 0,0 这也解释了为什么您的坐标大约为0,0
I would suggest replacing your control.setCenter(new GeoPoint((int)location.getLatitude(),(int)location.getLongitude()));
我建议更换您的control.setCenter(new GeoPoint((int)location.getLatitude(),(int)location.getLongitude()));
with control.setCenter(new GeoPoint((int)(location.getLatitude() * 1E6),(int)(location.getLongitude() * 1E6)));
与control.setCenter(new GeoPoint((int)(location.getLatitude() * 1E6),(int)(location.getLongitude() * 1E6)));
Try that, it should work then. 试试看,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.