在安卓端的谷歌地图上绘制多个标记点

4

我是一个安卓领域的新手。我已经编写了一个名为android App1的应用程序,该应用程序将从网络提供商检索纬度和经度值,并将其存储在我的本地服务器(LAMP)中。

我还创建了一个MYSQL数据库表,其中包含3个列(lat、lon、id),其中包含使用网络提供商检索的值(lat和lon)。目前我的表中有超过10个值。

我已经为使用PHP脚本从MYSQL DB获取这些值创建了JSON对象,该脚本位于我的Android App2中。

所有这些都很好运作。现在我要做的是创建一个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;

        }  
  }
      }
1个回答

2
这可能会有所帮助。在SO上也有很多例子。

实际上,我没有在代码中提供纬度和经度值。我正在尝试从MYSQL数据库中获取它们。那部分目前无法正常工作。 - user1917865
好的,那么 1)通过JSON/XML从MySQL数据库获取数据 2)解析JSON/XML并将获取的数据放入ArrayList中 3)循环开始。从ArrayList中获取lat、lng点并将其设置为GeoPoint。 4)使用该GeoPoint创建覆盖物项 5)将覆盖物项添加到mapView中。循环结束。这是一些代码http://pastebin.com/tq357RWR - Hein
我使用JSON从MYSQL获取了数据,并尝试检索纬度和经度值。但是我无法在地图上绘制所有点。只有最后一个纬度和经度值被绘制在地图上。请查看我的代码。我应该改变什么来获取所有值并在地图上绘制它。请帮忙。 - user1917865

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接