Reputation: 79
I can't fetch markers from coordinates stored in a mysql db. Where's the error in my codes?
P.S. LAT and LON fields in my table have VARCHAR type.
--MapsActivity.java
private GoogleMap mMap;
private Double Latitude = 0.00;
private Double Longitude = 0.00;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
ArrayList<HashMap<String, String>> location = new ArrayList<HashMap<String, String>>();
String url = "http://192.168.1.101/crud/markers_android.php";
try {
JSONArray data = new JSONArray(url);
location = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("LAT", c.getString("LAT"));
map.put("LON", c.getString("LON"));
location.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < location.size(); i++) {
Latitude = Double.parseDouble(location.get(i).get("LAT").toString());
Longitude = Double.parseDouble(location.get(i).get("LON").toString());
MarkerOptions marker = new MarkerOptions().position(new LatLng(Latitude, Longitude));
mMap.addMarker(marker);
}
}
}
file php markers_android.php is here
Upvotes: 1
Views: 78
Reputation: 1682
Use a log to see what response you get from json
for (int i = 0; i < data.length(); i++)
{
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("LAT", c.getString("LAT"));
map.put("LON", c.getString("LON"));
Log.w("eeee", "Lat : " + c.getString("LAT") + " Lon : " + c.getString("LON")); // add this line
location.add(map);
}
And give us the answer back if you can!
Upvotes: 1