Using database coordinates in google maps API

0

I'm trying to use in Google Maps the data that is in a sqlite database, but I'm not able to access the loader's return. I used the same logic to connect using the database number and it worked, but for the map, it only returns 0,0 coordinate. Follow the code, if someone can help me where I'm wrong, it's been a week since I'm stuck at that point.

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) {

}

}

    
asked by anonymous 09.08.2017 / 15:47

1 answer

2

The Loader returns the data asynchronously, so probably your map initializes before the Loader returns with the results. Try to initialize the map within the Loader's onLoadFinished () method after getting the coordinates.

    
09.08.2017 / 16:45