从数据库中获取坐标并创建标记

时间:2017-02-22 08:44:25

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

Goodmorning everyone!我的目标是从我的SQLite数据库中获取一组坐标,并将它们作为标记插入到地图中。 我尝试通过double但没有收集数据;然后我试着以这种方式取坐标:

public LatLng getLatLngMetod()
{
    SQLiteDatabase db=helper.getReadableDatabase();
    Cursor crs=db.query("tab", null, null, null, null, null, null);

    LatLng locati = null;

    while(crs.moveToNext())
    {
        String cord = crs.getString(crs.getColumnIndex("coordinate"));

        String[] latlong =  cord.split(",");
        double latitude = Double.parseDouble(latlong[0]);
        double longitude = Double.parseDouble(latlong[1]);
        locati = new LatLng(latitude, longitude);
    }
    return locati;
}
然后我尝试用这种方式调用方法:

mGoogleMap.addMarker(new MarkerOptions().position(dbManager.latLngMetod()));

我也将文件保存在数据库中:

            coordinateList = latLng.latitude + ", " + latLng.longitude;
            dbManager = new DbManager(getActivity());
            ContentValues contentValues = new ContentValues();
            contentValues.put("coordinate", coordinateList);
            dbManager.setProvider(contentValues);

错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.mattia.provaconfragment, PID: 31147
              java.lang.NullPointerException: Attempt to invoke interface method 'android.location.Location com.example.mattia.provaconfragment.OnDataUpdate.getPosition()' on a null object reference
                  at com.example.mattia.provaconfragment.PrimoFragment$override.updateMap(PrimoFragment.java:253)
                  at com.example.mattia.provaconfragment.PrimoFragment$override.access$dispatch(PrimoFragment.java)
                  at com.example.mattia.provaconfragment.PrimoFragment.updateMap(PrimoFragment.java:0)
                  at com.example.mattia.provaconfragment.MainActivity.onConnected(MainActivity.java:138)
                  at com.google.android.gms.common.internal.zzm.zzq(Unknown Source)
                  at com.google.android.gms.internal.zzaat.zzo(Unknown Source)
                  at com.google.android.gms.internal.zzaar.zzwi(Unknown Source)
                  at com.google.android.gms.internal.zzaar.onConnected(Unknown Source)
                  at com.google.android.gms.internal.zzaav.onConnected(Unknown Source)
                  at com.google.android.gms.internal.zzaag.onConnected(Unknown Source)
                  at com.google.android.gms.common.internal.zzl$1.onConnected(Unknown Source)
                  at com.google.android.gms.common.internal.zzf$zzj.zzxG(Unknown Source)
                  at com.google.android.gms.common.internal.zzf$zza.zzb(Unknown Source)
                  at com.google.android.gms.common.internal.zzf$zza.zzu(Unknown Source)
                  at com.google.android.gms.common.internal.zzf$zze.zzxH(Unknown Source)
                  at com.google.android.gms.common.internal.zzf$zzd.handleMessage(Unknown Source)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:160)
                  at android.app.ActivityThread.main(ActivityThread.java:5610)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)

0 个答案:

没有答案