Problem in the MapActivity
i m testing an app that uses the google maps api. My app is working but my problem is:
i want the gps to find my location and present it.If my gps in enabled,it wants some seconds to find my location.In these seconds, my app starts and uses the default long and lat..How could i add something like progress bar until my location found?thanks
this is a part of my code:
private double locationLat=37.979116;
private double locationLon=23.717766;
MapView mapView;
MapController mc;
GeoPoint p;
//..........
//in the onCreate
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,0,0, mlocListener);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// mapView.setSatellite(true);
// mapView.setStreetView(true);
mc = mapView.getController();
String lata = String.valueOf(locationLat);
String lnga = String.valueOf(locationLon);
String coordinates[] = {lata, lnga};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
//out of onCreate
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.avatar);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
//String Text = "My current location is: " +
//"Latitud = " + loc.getLatitude() +
//"Longitud = " + loc.getLongitude();
locationLat=loc.getLatitude();
locationLon=loc.getLongitude();
// Toast.makeText( getApplicationContext(),
//
// Text,
//
// Toast.LENGTH_SHORT).show开发者_JS百科();
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}/* End of Class MyLocationListener */
}/* End of Activity */
I would just put it into an ASyncTask and then pop up a Dialog Box saying finding your location...once found dismiss the dialog box...Google ASyncTask examples...that should help you out.
^^ Agreed with that not sure if you are just updating the overlay or calling your map controller to follow the lat and lon that gets updated but that is an idea you could use.
Also please read the documentation of "requestLocationUpdates" 0,0 will DESTROY a real phones battery. that is updating location every 0 milliseconds and/or 0 ft change.
精彩评论