i have tryed to make my own app and to use google maps. 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. Thanks everybody 😀
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. This also explains why you have coordinates of approximately 0,0
I would suggest replacing your
control.setCenter(new GeoPoint((int)location.getLatitude(),(int)location.getLongitude()));withcontrol.setCenter(new GeoPoint((int)(location.getLatitude() * 1E6),(int)(location.getLongitude() * 1E6)));Try that, it should work then.