Using database coordinates in google maps API

Asked

Viewed 213 times

0

I’m trying to use in Google Maps, the data that is in a database sqlite, But I’m having trouble accessing the shuttle’s return. I used the same logic to call using the database number and it worked, but for the map, it only returns 0.0 coordinate. Follow the code, if anyone can help me where I’m going wrong, because I’ve been stuck at that point for a week.

public class MapsDetailActivity extends FragmentActivity implements OnMapReadyCallback, LoaderManager.LoaderCallbacks<Cursor>{
private static final int EXISTING_DATA_LOADER = 0;
private Uri mCurrentUri;
double placeLat, placeLong;

private GoogleMap mMap;

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

    Intent intent = getIntent();
    mCurrentUri = intent.getData();
    getLoaderManager().initLoader(EXISTING_DATA_LOADER, null, this);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

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

    LatLng place = new LatLng(placeLat, placeLong);
    mMap.addMarker(new MarkerOptions().position(place));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {GuideContract.GuideEntry._ID,
            GuideContract.GuideEntry.COLUMN_PLACE_LATITUDE,
            GuideContract.GuideEntry.COLUMN_PLACE_LONGITUDE};
    return new CursorLoader(this,mCurrentUri, projection, null,null,null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (cursor.moveToFirst()){
        int latColumnIndex = cursor.getColumnIndex(GuideContract.GuideEntry.COLUMN_PLACE_LATITUDE);
        int lonColumnIndex = cursor.getColumnIndex(GuideContract.GuideEntry.COLUMN_PLACE_LONGITUDE);

        int latitude = cursor.getInt(latColumnIndex);
        int longitude = cursor.getInt(lonColumnIndex);

        placeLat = latitude;
        placeLong = longitude;

    }
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {

}

}

1 answer

2


The Loader returns the data asynchronously, so it probably initializes your map before the Loader returns with the results. Try initializing the map within the onLoadFinished() method of the Loader, after obtaining the coordinates.

  • Solved, thank you very much!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.