Get the current location of the Gps? Showing the default one

Posted by Gagandeep on Stack Overflow See other posts from Stack Overflow or by Gagandeep
Published on 2011-11-29T05:58:00Z Indexed on 2011/11/29 17:50 UTC
Read the original article Hit count: 268

Filed under:

Need help Urgent!!!!! Did changes with help but still unsuccessful...

I have to request location updates, but I am unsuccessful in implementing that... i modified the code but need help so that i can see the current location.

PLEASE look through my code and help please.. I am learning this and new to this concept and android.. any help would be appreciated

here is my code:

package com.GoogleMaps;

import java.util.List;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */

 private MapView mapView;
 private LocationManager lm;
 private LocationListener ll;
 private MapController mc;
 GeoPoint p = null;
 Drawable defaultMarker = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView)findViewById(R.id.mapview);
    //show zoom in/out buttons
    mapView.setBuiltInZoomControls(true);
    //Standard view of the map(map/sat)
    mapView.setSatellite(false);

    // get zoom tool
    mapView.setBuiltInZoomControls(true);
    //get controller of the map for zooming in/out
    mc = mapView.getController();
    // Zoom Level
    mc.setZoom(18); 

    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    ll = new MyLocationListener();

    lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            0,
            0,
            ll);


    //Get the current location in start-up
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    ll = new MyLocationListener();

    lm.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                0,
                0,
                ll);


    //Get the current location in start-up
    if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null){

         GeoPoint p = new GeoPoint(
               (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000),
               (int)(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000));
               mc.animateTo(p);
    }

    MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
    List<Overlay> list = mapView.getOverlays();
    list.add(myLocationOverlay);


}

protected class MyLocationOverlay extends com.google.android.maps.Overlay {


        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            Paint paint = new Paint();

            super.draw(canvas, mapView, shadow);
            GeoPoint p = null;


            // Converts lat/lng-Point to OUR coordinates on the screen.
            Point myScreenCoords = new Point();

            mapView.getProjection().toPixels(p, myScreenCoords);

            paint.setStrokeWidth(1);
            paint.setARGB(255, 255, 255, 255);
            paint.setStyle(Paint.Style.STROKE);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

            canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
            canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
            return true;
        }
    }

private class MyLocationListener implements LocationListener{

    public void onLocationChanged(Location argLocation) {


           // TODO Auto-generated method stub
           p = new GeoPoint((int)(argLocation.getLatitude()*1000000), (int)(argLocation.getLongitude()*1000000));

           Toast.makeText(getBaseContext(),
                   "New location latitude [" +argLocation.getLatitude() +
                   "] longitude [" + argLocation.getLongitude()+"]",
                   Toast.LENGTH_SHORT).show();

           mc.animateTo(p);
           mapView.invalidate(); // call this so UI of map was updated
          }


      public void onProviderDisabled(String provider) {
       // TODO Auto-generated method stub
      }

      public void onProviderEnabled(String provider) {
       // TODO Auto-generated method stub
      }

      public void onStatusChanged(String provider,
        int status, Bundle extras) {
       // TODO Auto-generated method stub
      }
     }    
protected boolean isRouteDisplayed() {
    return false;
}
}

catlog:

11-29 17:40:42.699: D/dalvikvm(371): GC_FOR_MALLOC freed 6074 objects / 369952 bytes in 74ms
11-29 17:40:42.970: I/MapActivity(371): Handling network change notification:CONNECTED
11-29 17:40:42.980: E/MapActivity(371): Couldn't get connection factory client
11-29 17:40:43.190: D/AndroidRuntime(371): Shutting down VM
11-29 17:40:43.190: W/dalvikvm(371): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-29 17:40:43.280: E/AndroidRuntime(371): FATAL EXCEPTION: main
11-29 17:40:43.280: E/AndroidRuntime(371): java.lang.NullPointerException
11-29 17:40:43.280: E/AndroidRuntime(371):  at com.google.android.maps.PixelConverter.toPixels(PixelConverter.java:71)
11-29 17:40:43.280: E/AndroidRuntime(371):  at com.google.android.maps.PixelConverter.toPixels(PixelConverter.java:61)
11-29 17:40:43.280: E/AndroidRuntime(371):  at com.GoogleMaps.MapsActivity$MyLocationOverlay.draw(MapsActivity.java:106)
11-29 17:40:43.280: E/AndroidRuntime(371):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
11-29 17:40:43.280: E/AndroidRuntime(371):  at com.google.android.maps.MapView.onDraw(MapView.java:494)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.View.draw(View.java:6740)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.View.draw(View.java:6743)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.View.draw(View.java:6743)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
11-29 17:40:43.280: E/AndroidRuntime(371):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewRoot.draw(ViewRoot.java:1407)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.os.Looper.loop(Looper.java:123)
11-29 17:40:43.280: E/AndroidRuntime(371):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-29 17:40:43.280: E/AndroidRuntime(371):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 17:40:43.280: E/AndroidRuntime(371):  at java.lang.reflect.Method.invoke(Method.java:521)
11-29 17:40:43.280: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-29 17:40:43.280: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-29 17:40:43.280: E/AndroidRuntime(371):  at dalvik.system.NativeStart.main(Native Method)
11-29 17:40:45.779: D/dalvikvm(371): GC_FOR_MALLOC freed 5970 objects / 506624 bytes in 1179ms
11-29 17:40:45.779: I/dalvikvm-heap(371): Grow heap (frag case) to 3.147MB for 17858-byte allocation
11-29 17:40:45.870: D/dalvikvm(371): GC_FOR_MALLOC freed 56 objects / 2304 bytes in 92ms
11-29 17:40:45.960: D/dalvikvm(371): GC_EXPLICIT freed 3459 objects / 196432 bytes in 74ms
11-29 17:40:48.310: D/dalvikvm(371): GC_EXPLICIT freed 116 objects / 41448 bytes in 68ms
11-29 17:40:49.540: I/Process(371): Sending signal. PID: 371 SIG: 9

© Stack Overflow or respective owner

Related posts about android