我是android领域的新手。我编写了一个 android App1 ,它将从网络提供程序中检索纬度和经度值,并将其存储在我的本地服务器(LAMP)中。
我还创建了一个MYSQL数据库表,它有3列(lat,lon,id),它们具有使用网络提供程序检索的值(lat和lon)。目前我的表中有超过10个值。
我创建了JSON对象,用于在 Android App2
中使用PHP脚本从MYSQL DB获取这些值所有这些都很好。我现在要做的是创建一个MapActivity,它将使用Marker在地图上绘制那些lat和lon值。我试图在地图上绘制它,但我在地图上只得到我的最后一个位置(我的表中的最后一个lat和lon值)。
如何在地图上绘制我的所有点数。以下是我尝试的代码,但它无法正常工作。请帮我纠正我的错误。
public class ShowMapActivity extends MapActivity {
String returnString;
String returnString1;
double aa1 ;
double aa2;
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlay itemizedoverlay;
private MyLocationOverlay myLocationOverlay;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// Configure the Map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
itemizedoverlay = new MyOverlay(this, drawable);
createMarker();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private void createMarker() {
GeoPoint p = mapView.getMapCenter();
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
if (itemizedoverlay.size() > 0) {
mapView.getOverlays().add(itemizedoverlay);
}
}
@Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
@Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
public class GetMethodEx {
public String getInternetData() throws Exception {
BufferedReader in = null;
String data = "";
//String returnString = null;
// httpGet
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://192.168.1.15/latlonret1.php");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
// return data;
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try {
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
JSONObject json_data1 = jArray.getJSONObject(i);
returnString =json_data.getString("lat") + "\n";
returnString1 =json_data1.getString("lon") + "\n";
System.out.println(returnString);
System.out.println(returnString1);
}
Bundle bundle = new Bundle();
bundle.putString("stuff", returnString);
bundle.putString("stuff1", returnString1);
try
{
aa1=Double.valueOf(returnString);
aa2=Double.valueOf(returnString1);
}
catch(Exception e)
{
}
System.out.println(returnString);
System.out.println(returnString1);
}
catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return returnString;
}
}
}