Reputation: 31
beginner here. I have created an activity in Android Studio with a map fragment, and I have no issue of dynamically adding 1 marker or adding several markers non-dynamically like so:
LatLng sydney = new LatLng(-34, 151);
LatLng spokane = new LatLng(-35, 171);
LatLng idaho = new LatLng(-32.3, 150);
However, I need to set an array of markers dynamically, after grabbing the coordinates from mysql.
Right now, for testing purposes, I have a button 'getCoords' that executes this on click(I have the coords outputting into a textview to see if it works. I am able to get the values to show in the textview):
public void getCoords(View view){
new CoordsBackgroundTask().execute();
}
class CoordsBackgroundTask extends AsyncTask<Void, Void, String>{
String json_url_coords;
@Override
protected void onPreExecute(){
json_url_coords = "MY PHP FILE URL";
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(json_url_coords);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while ((JSON_STRING = bufferedReader.readLine())!=null)
{
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values){
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
TextView textview = (TextView) findViewById(R.id.textview);
textview.setText(result);
json_string = result;
}
}
The PHP:
<?php
$mysqli = new mysqli("localhost", "MYUSER", "MYPASS", "MYDB");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT markers.lat, markers.lng
FROM markers
INNER JOIN userIds ON markers.user_id = userIds.user_id";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row['lat'];
echo $row['lng'];
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
I'm able to successfully return the lat and lng array, i'm just having a hard time from here, as to how to, on click of the button that gets the coords from the database, to THEN add those markers to the map fragment on that activity. Do I need to parse the array somehow? Can I use a loop to bring the markers into the map? There will always be a different number of sets of coords.
My map looks sort of like this. I have it in setUpMap so I can have it called onMapReady as per default, and then call it back when the button is clicked somehow...
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
private void setUpMap() {
LatLng sydney = new LatLng(-34, 151);
LatLng spokane = new LatLng(-35, 171);
LatLng idaho = new LatLng(-32.3, 150);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.addMarker(new MarkerOptions().position(spokane).title("Marker in spokane"));
mMap.addMarker(new MarkerOptions().position(idaho).title("Marker in idaho"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
Thank you!
Upvotes: 0
Views: 1931
Reputation: 343
you would probably need a for loop to insert the markers, can you post the PHP response so I can help you implementing something to parse that to a List of LatLng and then add a marker for each one.
ANSWER
Assuming your points are lines like lat,lng,# -32.430412,-58.321323,#
, and your server gives you a list of them you first split it like:
String answer = "-32.430412,-58.321323,#-32.430412,-58.321323,#-32.430412,-58.321323,#";
// note that line breaks disappeared after parsing it to string
String[] parts = answer.split("#");
for (String point : parts) {
String[] pointData = point.split(",");
Float lat= Float.parseFloat(pointData[0]);
Float lng= Float.parseFloat(pointData[1]);
/// check if fit your actual map, remember you need everything required here loaded and declared before calling.
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Hello world"));
}
Upvotes: 1