我无法从存储在mysql数据库中的坐标中获取标记。我的代码中的错误在哪里?
P.S。我表中的LAT和LON字段具有VARCHAR类型。
- 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);
}
}
}
文件php markers_android.php是here
答案 0 :(得分:1)
使用日志查看您从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);
}
如果可以,请给我们答案!