0

I am trying to learn the basics of location services and putting "pins" to mark locations.

I'm not sure whether this is the best approach, or what the alternatives are. What would be a better, or just a simpler way of doing this? Or what is the standard way?

Additionally, when I run this app on ICS 4.0.3, the phone just freezes after a few minutes. It works fine at first, and shows me the location, but eventually the entire device becomes unresponsive. I've checked both logcat and last_kmsg, and I can't find any hints about what is causing this. Gingerbread 2.3.6 runs the code without problems (at least it didn't crash for over 15min, while ICS crashes within 1~5min).

It doesn't seem like an exception, so maybe this is some memory leak or a kernel panic? I'm not sure how to start debugging this :\

Any help would be appreciated!

I am using the following code:

Mapper.java:

public class Mapper extends MapActivity{

MapView mapView;
LocationManager lm;
LocationListener ll;
GeoPoint p;
MapController mc;
MapOverlay overlay;
List<Overlay> lol;

public void onCreate(Bundle x){
    super.onCreate(x);
    setContentView(R.layout.mapper);

    mapView = (MapView)findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);
    mc = mapView.getController();
    lol = mapView.getOverlays();
    overlay = new MapOverlay();
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    ll = new MyLocationListener();

    mapView.invalidate();
}


private class MyLocationListener implements LocationListener{
    public void onLocationChanged(Location loc){
        if(loc!=null){
            int lat = (int)(loc.getLatitude() * 1E6);
            int lon = (int)(loc.getLongitude() * 1E6);
            p = new GeoPoint(lat,lon);

            mc.animateTo(p);
            mc.setZoom(18);

            lol.clear();
            lol.add(overlay);
        }
    }


    @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

    }
}

public void onResume(){
    super.onResume();

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
}

public void onPause(){
    super.onPause();

    lm.removeUpdates(ll);
}

protected boolean isRouteDisplayed(){
    return false;
}

private class MapOverlay extends com.google.android.maps.Overlay{
    public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when){
        super.draw(canvas, mv, shadow);

        Point sp = new Point();
        mv.getProjection().toPixels(p, sp);
        Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
        canvas.drawBitmap(pic, sp.x, sp.y, null);
        canvas.drawBitmap(pic, sp.x+60, sp.y, null);
        canvas.drawBitmap(pic, sp.x-60, sp.y, null);
        canvas.drawBitmap(pic, sp.x, sp.y+60, null);
        canvas.drawBitmap(pic, sp.x, sp.y-60, null);
        return true;
    }
}
}

mapper.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <com.google.android.maps.MapView
      android:id="@+id/mapView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:apiKey="mykeyworks"
      android:clickable="true" />

</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jinworld"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>"

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="Second Activity" >
        <intent-filter>
            <action android:name="com.jinworld.SecondActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Notify"
        android:label="DEVELOPERS" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Mapper"
        android:label="Locatio" >
        <intent-filter>
            <action android:name="com.jinworld.Mapper" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

</manifest>
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
QQ_QQ
  • 123
  • 1
  • 7

1 Answers1

1

You don't need so much code to display your location, google already has an Overlay for this. Try this, it may helps:

public class Mapper extends MapActivity{
    com.google.android.maps.MyLocationOverlay myLocationOverlay;

    @Override
    protected void onResume() {
        super.onResume();
        myLocationOverlay.enableMyLocation();
    }

    @Override
    protected void onPause() {
        super.onPause();
        myLocationOverlay.disableMyLocation();
    }

    @Override
    public void onCreate(Bundle x){
        ...
        setupMyLocation();
        ...
    }

    protected void setupMyLocation(){
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                mapView.getOverlays().add(myLocationOverlay);
            }
        });
    }
}
Plutoz
  • 694
  • 9
  • 13