代码之家  ›  专栏  ›  技术社区  ›  ghasem deh

android maps显示了多个标记(来自sqlite的LatLng)

  •  -1
  • ghasem deh  · 技术社区  · 7 年前

    我在android应用程序中使用谷歌地图和get(用户)设备位置(GPS位置),并将其插入数据库(SQLite)经纬度和地址!

    现在我想显示多个位置与LatLng读取从数据库…没有问题,在创建标记,但在标记信息(国家,城市…)只显示最后插入的所有标记的位置!

    这是我的代码:

    private void displayMultiplePoint() {
            if (LOCATION_TABLE.size() > 0) {
                for (int i = 0; LOCATION_TABLE.size() > i; i++) {
                    int id = LOCATION_TABLE.get(i).getId();
                    lat = LOCATION_TABLE.get(i).getLatitude();
                    lng = LOCATION_TABLE.get(i).getLongitude();
                    place = LOCATION_TABLE.get(i).getPlace();
                    rate = LOCATION_TABLE.get(i).getRate();
                    drawMarker(new LatLng(lat, lng), "city", place, rate);
                    displayToast(id + "" + place);
                }
            }
        }
    
        private void drawMarker(final LatLng point, final String city, final String place, final float rate) {
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker arg0) {
                    return null;
                }
    
                @Override
                public View getInfoContents(Marker arg0) {
                    View v = null;
                    try {
                        v = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
                        ImageView map_image = (ImageView) v.findViewById(R.id.maps_image);
                        map_image.setImageResource(R.drawable.runhd);
                        TextView city_txt = (TextView) v.findViewById(R.id.maps_city);
                        city_txt.setText(city);
                        TextView place_txt = (TextView) v.findViewById(R.id.maps_place);
                        place_txt.setText(place);
                        RatingBar rate_bar = (RatingBar) v.findViewById(R.id.exercise_display_rate);
                        rate_bar.setRating(rate);
                    } catch (Exception ev) {
                        System.out.print(ev.getMessage());
                    }
                    return v;
                }
            });
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(point);
            mMap.addMarker(markerOptions);
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 6));
        }
    

    我在数据库中显示了lcation表中的toast form rowId,并显示了3行id:1、2、3,但在marker info中显示了最后一个id(id号:3)

    这是我的片段:

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  1
  •   user6490462 user6490462    7 年前

    对于您的案例,我有很多解决方案,但首先:

    你的坠落 setInfoWindowAdapter 方法只是 调用一次 drawMarker

     for (int i = 0; LOCATION_TABLE.size() > i; i++) {
                int id = LOCATION_TABLE.get(i).getId();
                lat = LOCATION_TABLE.get(i).getLatitude();
                lng = LOCATION_TABLE.get(i).getLongitude();
                place = LOCATION_TABLE.get(i).getPlace();
                rate = LOCATION_TABLE.get(i).getRate();
                drawMarker(new LatLng(lat, lng), "city", place, rate);
                displayToast(id + "" + place);
                ..........
                 @Override
            public View getInfoContents(Marker arg0) {
                View v = null;
                try {
                    v = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
                    ImageView map_image = (ImageView) v.findViewById(R.id.maps_image);
                    map_image.setImageResource(R.drawable.runhd);
                    TextView city_txt = (TextView) v.findViewById(R.id.maps_city);
                    city_txt.setText("city");
                    TextView place_txt = (TextView) v.findViewById(R.id.maps_place);
                    place_txt.setText(place);
                    RatingBar rate_bar = (RatingBar) v.findViewById(R.id.exercise_display_rate);
                    rate_bar.setRating(rate);
                } catch (Exception ev) {
                    System.out.print(ev.getMessage());
                }
                return v;
                ......
            }
    

    使用 Cursor 通过你的数据库,在任何地方使用它(这将是了不起的)。

    第三 Clustering 中的算法 google_maps-utils-example .

        2
  •  1
  •   BhalchandraSW    7 年前

    setInfoWindowAdapter() 为整个地图设置信息窗口。在…内 setInfoWindowAdapter()

    1. 您需要维护一个标记到数据的映射。

      Map<Marker, Place> markerToPlaceMap = new HashMap<>();
      

      Place

      class Place {
          public String city, place;
          public float rating;
      }
      

      注意:请将成员更改为private,并根据您的适合性实现getter和setter。

    2. 接下来,你的 drawMarker() 将更改如下。它需要添加 marker place markerToPlace 地图

      private void drawMarker(final LatLng point, final String city, final String place, final float rate) {
          MarkerOptions markerOptions = new MarkerOptions();
          markerOptions.position(point);
          Marker marker = mMap.addMarker(markerOptions);
          mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 6));
          markerToPlaceMap.put(marker, new Place(city, place, rating));
      }
      
    3. 最后,您将覆盖 GoogleMap.setInfoWindowAdapter() 位置 与a相关 标记

      mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
      
          @Override
          public View getInfoWindow(Marker marker) {
              return null;
          }
      
          @Override
          public View getInfoContents(Marker marker) {
              View v = null;
              try {
      
                  v = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
      
                  ImageView map_image = (ImageView) v.findViewById(R.id.maps_image);
                  map_image.setImageResource(R.drawable.runhd);
      
                  TextView city_txt = (TextView) v.findViewById(R.id.maps_city);
                  city_txt.setText(markerToPlace.get(marker).city); // <- Check how corresponding place for a marker is fetched and used
      
                  TextView place_txt = (TextView) v.findViewById(R.id.maps_place);
                  place_txt.setText(markerToPlace.get(marker).place);
      
                  RatingBar rate_bar = (RatingBar) v.findViewById(R.id.exercise_display_rate);
                  rate_bar.setRating(markerToPlace.get(marker).rate);
      
              } catch (Exception ev) {
                  System.out.print(ev.getMessage());
              }
              return v;
          }
      });
      
        3
  •  0
  •   ghasem deh    7 年前

    但是 我的代码更改为:

    private void displayMultiplePoint() {
            Cursor cursor = DB_HELPER.LOCATION_MARKERS();
            if (cursor.moveToFirst()) {
                for (int i = 0; cursor.getCount() > i; i++) {
                    int id = cursor.getInt(cursor.getColumnIndex("id"));
                    double lat = cursor.getDouble(cursor.getColumnIndex("latitude"));
                    double lng = cursor.getDouble(cursor.getColumnIndex("longitude"));
                    String place = cursor.getString(cursor.getColumnIndex("place"));
                    float rate = cursor.getFloat(cursor.getColumnIndex("rate"));
                    displayToast(id + ", " + place + rate);
                    cursor.moveToNext();
                    drawMarker(new LatLng(lat, lng));
                }
                cursor.close();
            }
        }
    

    我得到arg0.getId表单:(m0,m1,m2…)

    public View getInfoContents(Marker arg0)