我是android领域的新手。我编写了一个android App1,它将从 Network Provider 检索纬度和经度值并将其存储在我的本地服务器(LAMP)中。
我还创建了一个 MYSQL DB 表,该表具有 3 列(lat、lon、id),其中包含使用网络提供程序检索的值(lat 和 lon)。目前我的表中有 10 多个值。
我创建了 JSON 对象,用于在我的Android App2中使用 PHP 脚本从 MYSQL DB 获取这些值
所有这些事情都很好。我现在要做的是创建一个 MapActivity,它将使用 Marker 在地图上绘制这些纬度和经度值。我试图在地图上绘制它,但我在地图上只获得了我的最后一个位置(我表中的最后一个纬度和经度值)。
如何在地图上绘制我的所有点。以下是我尝试过的代码,但无法正常工作。请帮助我纠正我的错误。
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;
}
}
}