1

There appear to be a number of similar questions on this subject, but they either use a different programming language (I am programming with Java) or don't answer my question.

I am using Android Studio to create a Google Maps application. I would like to create markers on the map, by reading in values from a database. Those values will be for a marker's latitude and longitude (i.e. the marker's precise location).

I have created a database and added values into it (I think I've done this correctly - I've been following lots of online tutorials). I also have the Google map successfully working. But I am stuck on how to get the map to read the latitude and longitude values from the database, so that it can generate markers at those corresponding locations.

In Android Studio, I have a GoogleMapsActivity. I have several java classes for the database. I have an AndroidManifest.xml and a google_maps_api.xml. This is all in one "package" (if any of this information helps).

Any ideas for how to do this?

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
DinnerPlate
  • 17
  • 1
  • 8

1 Answers1

1

Unfortunately there isn't enough information in the question to be able to provide a complete solution. You did not mention which database you are using, the format of the data, or what exactly you are having a problem with.

I'm going to assume that you are able to query the data from the database and have your GoogleMap set up. I'll show you how to use those values to add markers to your map. In your GoogleMapsActivity, you should have an onMapReady method. You can use that to add markers as following:

public void onMapReady(GoogleMap map) {
     MapsInitializer.initialize(mMapView.getContext());
     for (CustomLocation location : getLocations()) {
          LatLng latLng = new LatLng(location.lat, location.lng);
          map.addMarker(new MarkerOptions().position(latLng));
     }
}

public List<CustomLocation> getLocations() {
     // here you read from your database to get your CustomLocation object 
     // containing latitude and longitude
}

Note, depending on the database you are using and the amount of data you are querying, you may want to call getLocations() on a background thread and process the result on a UI thread. If you're using something like Realm and you're not querying a large dataset, then it may be ok to keep it on the UI thread.

vanguard
  • 147
  • 1
  • 7