从SQLite数据库在Google Map上标记位置(纬度,经度)

时间:2018-01-17 09:48:19

标签: android google-maps android-sqlite

我正试图在Android Studio上的SQLite数据库中标记Google地图上的位置。任何人都建议从SQLite数据库中检索位置,然后在Google Map上标记这些位置。

2 个答案:

答案 0 :(得分:0)

在您的活动中实施 OnMapReadyCallback ,并在 onMapReady 回调中设置标记。

<强>例如

  public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        //for setting custom marker
        Drawable circleDrawable = getResources().getDrawable(R.drawable.map_placeholder);
        BitmapDescriptor icon = getMarkerIconFromDrawable(circleDrawable);

        //create LatLng object using value got from sqlite db
        LatLng selectedPlace = new LatLng(mLat, mLong);
        mMap.addMarker(new MarkerOptions().position(selectedPlace).title(mName).icon(icon));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(selectedPlace, 10));
    }

}

注意:您需要在app模块的build.gradle中添加play-service-map依赖项

答案 1 :(得分:0)

我只想分享我的答案。它运作成功:

public void show(View view) {
    Cursor cursor = db.rawQuery( "select lat,lng from places", null );
    String []latitude=new String[3];
    String []longitude=new String[3];
    double []dlatitude= new double[3];
    double []dlongitude= new double[3];

    if(cursor.moveToFirst()){
        for(int i=0;i<3;i++){
            latitude[i]=cursor.getString(cursor.getColumnIndex( "lat" ));
            dlatitude[i]=Double.parseDouble( latitude[i] );

            longitude[i]=cursor.getString( cursor.getColumnIndex( "lng" ) );
            dlongitude[i]=Double.parseDouble( longitude[i] );
            cursor.moveToNext();
        }
    }

    for(int j=0;j<latitude.length;j++) {

        LatLng latLng = new LatLng( dlatitude[j], dlongitude[j] );
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position( latLng );
        markerOptions.title( "current location" );
        mMap.addMarker( markerOptions );
        mMap.animateCamera( CameraUpdateFactory.newLatLng( latLng ) );
        markerOptions.draggable( true );
    }
}