0

我正在尝试学习定位服务的基础知识并使用“大头针”来标记位置。

我不确定这是否是最好的方法,或者有什么替代方法。有什么更好的,或者只是更简单的方法呢?或者标准方法是什么?

此外,当我在 ICS 4.0.3 上运行此应用程序时,手机会在几分钟后冻结。起初它工作正常,并向我显示位置,但最终整个设备变得无响应。我已经检查了 logcat 和 last_kmsg,但我找不到任何关于导致此问题的提示。Gingerbread 2.3.6 运行代码没有问题(至少它没有崩溃超过 15 分钟,而 ICS 在 1~5 分钟内崩溃)。

这似乎不是一个例外,所以也许这是一些内存泄漏或内核恐慌?我不知道如何开始调试这个:\

任何帮助,将不胜感激!

我正在使用以下代码:

映射器.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;
    }
}
}

映射器.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>
4

1 回答 1

1

您不需要太多代码来显示您的位置,谷歌已经为此提供了一个覆盖。试试这个,它可能会有所帮助:

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);
            }
        });
    }
}
于 2012-05-18T09:47:41.600 回答