【问题标题】:Adding multiple markers on Google map V2在 Google 地图 V2 上添加多个标记
【发布时间】:2013-09-20 13:12:20
【问题描述】:

您好,我正在开发小型 android 应用程序,我想在其中显示地图和地图上的一些标记。我有 latlang 值列表,我想在地图上显示它。我尝试了以下方式:

for(int pin=0; pin<pins.size(); pin++)
            {
                LatLng pinLocation = new LatLng(Float.parseFloat(pins.get(pin).latitude), Float.parseFloat(pins.get(pin).longitude));
                Marker storeMarker = map.addMarker(new MarkerOptions()
                .position(pinLocation)
                .title(pins.get(pin).pinname)
                .snippet(pins.get(pin).address)
                );
            }

所以我的问题是,当我尝试上述方法时,它只显示最后一个标记而不显示所有标记。这个怎么做。需要帮忙。谢谢。

【问题讨论】:

标签: android google-maps google-maps-markers


【解决方案1】:
    for(int pin=0; pin<pins.size(); pin++)
                {
                    LatLng pinLocation = new LatLng(Float.parseFloat(pins.get(pin).latitude), Float.parseFloat(pins.get(pin).longitude));
                    Marker storeMarker = map.addMarker(new MarkerOptions()
                    .position(pinLocation )-->here i had made some changes add "pinLocation" instead of "storeLocation"
                    .title(pins.get(pin).pinname)
                    .snippet(pins.get(pin).address)
                    );
                }

> and after first check size of pins.size() ..

【讨论】:

  • Altaf 我尝试了您的解决方案,但仍然无法正常工作。有没有其他办法
  • 你检查你的数组大小了吗??
  • 是的,先生,数组的大小是 2。我仍然不知道它只显示了最后一个标记。
  • @nilkash 我也有同样的问题。你解决了吗?
【解决方案2】:

你可以这样使用:

for (int i = 0; i < pins.size(); i++) {

 double lati=Double.parseDouble(pins.get(i).latitude);
 double longLat=Double.parseDouble(pins.get(i).longitude);
 MAP.addMarker(new MarkerOptions().
 position(
 new LatLng(lati,longLat)).title(pins.get(i).pinname).snippet(pins.get(i).address));


 }

【讨论】:

    【解决方案3】:

    使用如下共享偏好

      // Opening the sharedPreferences object
            sharedPreferences = getSharedPreferences("location", 0);
    
            // Getting number of locations already stored
            locationCount = sharedPreferences.getInt("locationCount", 0);
    
            // Getting stored zoom level if exists else return 0
            String zoom = sharedPreferences.getString("zoom", "0");
    
            // If locations are already saved
            if(locationCount!=0){
    
                String lat = "";
                String lng = "";
    
                // Iterating through all the locations stored
                for(int i=0;i<locationCount;i++){
    
                    // Getting the latitude of the i-th location
                    lat = sharedPreferences.getString("lat"+i,"0");
    
                    // Getting the longitude of the i-th location
                    lng = sharedPreferences.getString("lng"+i,"0");
    
                    // Drawing marker on the map
                    drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
                }
    

    用于标记标记:

                 SharedPreferences.Editor editor = sharedPreferences.edit();
    
                // Storing the latitude for the i-th location
                editor.putString("lat"+ Integer.toString((locationCount-1)), Double.toString(point.latitude));
    
                // Storing the longitude for the i-th location
                editor.putString("lng"+ Integer.toString((locationCount-1)), Double.toString(point.longitude));
    
                // Storing the count of locations or marker count
                editor.putInt("locationCount", locationCount);
    
                /** Storing the zoom level to the shared preferences */
                editor.putString("zoom", Float.toString(googleMap.getCameraPosition().zoom));
    
                /** Saving the values stored in the shared preferences */
                editor.commit();
    
                Toast.makeText(getBaseContext(), "Marker is added to the Map", Toast.LENGTH_SHORT).show();
    

    Refer this one...source code is availbale

    【讨论】:

      【解决方案4】:
      for (int i = 0; i < ComponentMapList.size(); i++) {
                          String city = ComponentMapList.get(i).getCity();
                          if (city != null) {
                              center = CameraUpdateFactory.newLatLng(new LatLng(
                                      ComponentMapList.get(i).getLat(),
                                      ComponentMapList.get(i).getLng()));
                              mMap.addMarker(new MarkerOptions().position(
                                      new LatLng(ComponentMapList.get(i).getLat(),
                                              ComponentMapList.get(i).getLng()))
                                      .title(city));
                          }
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-26
        • 2013-03-10
        • 1970-01-01
        • 1970-01-01
        • 2012-12-23
        • 2014-12-24
        • 1970-01-01
        • 2013-03-14
        相关资源
        最近更新 更多