【问题标题】:Plotting multiple markers on google map in android在android中的谷歌地图上绘制多个标记
【发布时间】:2012-12-20 06:42:17
【问题描述】:

我是安卓领域的新手。我编写了一个 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;

        }  
  }
      }

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    This 可能会有所帮助。 SO也有很多例子。

    【讨论】:

    • 实际上我并没有在代码中给出 lat 和 lon 值。我正在尝试从 MYSQL DB 中获取它。 Tat 部分不工作
    • 好吧,在这种情况下 1) 通过 JSON/XML 从 MySQL DB 获取数据 2) 解析 JSON/XML 并将获取的数据放入 Arraylist 3) 循环开始。从 arraylist 中获取 lat,lng 点并将其设置为 GeoPoint。 4) 使用该 GeoPoint 创建覆盖项 5) 将覆盖项添加到 mapView。循环结束。这是一些代码pastebin.com/tq357RWR
    • 是的,我使用 JSON 从 MYSQL 获取数据并尝试检索 lat 和 lon 值。但我无法在地图上绘制所有点。只有最后一个纬度和经度值绘制在地图上。请参阅我的代码。我应该改变什么来获取所有值并将其绘制在地图上。请帮忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2015-05-27
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    相关资源
    最近更新 更多