Google地图标记显示在错误的位置

时间:2011-08-21 13:28:20

标签: android sqlite google-maps google-maps-markers

我想显示从SQLite数据库中检索的一些点/坐标。奇怪的是,当我发布地图活动时,我在海边,加蓬和尼日利亚附近的一个陌生的地方展示了标记:http://imageshack.us/f/402/gabond.png/

我尝试手动设置地图以给定坐标为中心,并始终存在同样的问题。另一件事,我在启动活动时遇到了这个logcat错误:

08-21 13:19:59.326: ERROR/MapActivity(404): Couldn't get connection factory client

代码:

CoordBD CoordBd = new CoordBD(this);
        CoordBd.open();

        Coordo coordo = new Coordo(36.876673,10.325928);
        Coordo coordo2 = new Coordo(36.876982,10.326228);
        //CoordBd.open();
        CoordBd.insertCoordo(coordo);
        CoordBd.insertCoordo(coordo2);
        // CoordBd.close();

        maMap = (MapView)findViewById(R.id.myGmap);
        maMap.setBuiltInZoomControls(true);
        ItemizedOverlayPerso pinOverlay = new ItemizedOverlayPerso(getResources().getDrawable(R.drawable.marker));

        /*db = openOrCreateDatabase(
            "coord.bd"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
            );
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);*/

        String[] result_columns = new String[] {COL_LATI, COL_LONGI};
        Cursor cur = db.query(true, TABLE_COORD, result_columns,
        null, null, null, null, null, null);
        cur.moveToFirst();
        while (cur.isAfterLast() == false) {
            int latitude = cur.getColumnIndex("latitude");
            int longitude = cur.getColumnIndex("longitude");
            point = new GeoPoint(microdegrees(latitude),microdegrees(longitude));
            pinOverlay.addPoint(point);
            cur.moveToNext();
        }
        cur.close();
        CoordBd.close();

        maMap.getOverlays().add(pinOverlay);
        monControler = maMap.getController();
        monControler.setZoom(12);
        monControler.setCenter(point);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);

可能是问题的根源是什么? PS:我在手机上安装应用程序时遇到同样的问题。 谢谢您的帮助。 编辑:Logcat:(用挂钩答案修改后):

08-22 01:55:35.381: ERROR/AndroidRuntime(253): Uncaught handler: thread main exiting due to uncaught exception
08-22 01:55:35.402: ERROR/AndroidRuntime(253): java.lang.RuntimeException: Unable to start activity ComponentInfo{carburant.android.com/carburant.android.com.Geo}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.os.Looper.loop(Looper.java:123)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at java.lang.reflect.Method.invokeNative(Native Method)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at java.lang.reflect.Method.invoke(Method.java:521)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at dalvik.system.NativeStart.main(Native Method)
08-22 01:55:35.402: ERROR/AndroidRuntime(253): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.database.CursorWindow.getDouble_native(Native Method)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.database.CursorWindow.getDouble(CursorWindow.java:399)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:138)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at carburant.android.com.Geo.onCreate(Geo.java:92)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-22 01:55:35.402: ERROR/AndroidRuntime(253):     ... 11 more

EDIT: Added class creating the DB:

public class MaBaseSQLite extends SQLiteOpenHelper {
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_COORD + " ("
    + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LATI + " TEXT NOT NULL, "
    + COL_LONGI + " TEXT NOT NULL);";

    MaBaseSQLite(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //on créé la table à partir de la requête écrite dans la variable CREATE_BDD
        db.execSQL(CREATE_BDD);
    }

1 个答案:

答案 0 :(得分:2)

在while循环中设置断点,您可能会注意到纬度和经度都是0或0和1.问题在于以下两行。你得到的是列索引,而不是实际值。你还想把你的坐标存储为双精度而不是整数:

int latitude = cur.getColumnIndex("latitude");
int longitude = cur.getColumnIndex("longitude");

尝试将其设置为:

double latitude = cur.getDouble(cur.getColumnIndex("latitude"));
double longitude = cur.getDouble(cur.getColumnIndex("longitude"));