2014-06-06 6 views
0

나는이 내가 시도 할 것입니다 MarkerCluster사용자 정보

를 사용하여 각 마커에 대해 서로 다른 값으로 사용자 정보창을 렌더링하기 위해 노력하고 있어요 :

private final GoogleMap.InfoWindowAdapter mInfoWindowAdapter = new GoogleMap.InfoWindowAdapter() { 
     @Override 
     public View getInfoWindow(Marker marker) { 
      View window = null; 
      if(getActivity()!=null&&isAdded()){ 
       window = getActivity().getLayoutInflater().inflate(R.layout.map_objective_overlay, null); 
       final CustomFontTextView nameTV = (CustomFontTextView) window.findViewById(R.id.nameTV); 

       if(clickedClusterItem!=null){ 
        System.out.println("You clicked this: "+clickedClusterItem.getName()); 
       }else{ 
        System.out.println("The clicked cluster item was nulllll"); 
       } 
       if (clickedCluster != null) { 
        for (Objective item : clickedCluster.getItems()) { 
         // Extract data from each item in the cluster as needed 
         if(item.getRemoteId().equals(clickedClusterItem.getRemoteId())){ 
          nameTV.setText(clickedClusterItem.getName()); 
         } 
        } 
       } 
      } 
      return window; 
     } 

     @Override 
     public View getInfoContents(Marker marker) { 
      return null; 
     } 
    }; 
    private Objective clickedClusterItem; 
    private Cluster<Objective> clickedCluster; 

@Override 
    public void onResume() { 
     super.onResume(); 
     preferences = getActivity().getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE); 
     if (map == null) { 
      map = fragment.getMap(); 
      mClusterManager = new ClusterManager<Objective>(getActivity(), map); 
      map.setOnCameraChangeListener(mClusterManager); 
      map.setOnMarkerClickListener(mClusterManager); 
      latitude = Double.parseDouble(preferences.getString(Constants.LATITUDE, "0")); 
      longitude = Double.parseDouble(preferences.getString(Constants.LONGITUDE, "0")); 
      map.addMarker(new MarkerOptions() 
        .position(new LatLng(latitude, longitude)) 
        .title("Hello " + preferences.getString(Constants.TOURIST_NAME, "tourist")) 
        .snippet("You are here") 
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_check_in))); 
      map.setInfoWindowAdapter(mClusterManager.getMarkerManager()); 
      //mClusterManager.setRenderer(new ObjectiveClusterRenderer(getActivity(), map, mClusterManager)); 
      mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(mInfoWindowAdapter); 
      mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(mInfoWindowAdapter); 
      map.setOnMarkerClickListener(mClusterManager); 

      mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<Objective>() { 
       @Override 
       public boolean onClusterClick(Cluster<Objective> cluster) { 
        clickedCluster = cluster; // remember for use later in the Adapter 
        return false; 
       } 
      }); 
      mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<Objective>() { 
       @Override 
       public boolean onClusterItemClick(Objective item) { 
        clickedClusterItem = item; 
        return false; 
       } 
      }); 

그리고 여기가 clustermanager에 마커를 추가 :

public void setUpClusterer(List<Objective> objectivesList) { 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10)); 
     addItems(objectivesList); 
    } 

    private void addItems(List<Objective> objectiveList) { 
     for (Objective anObjectiveList : objectiveList) { 
      Objective offsetItem = new Objective(); 
      offsetItem.setRemoteId(anObjectiveList.getRemoteId()); 
      offsetItem.setName(anObjectiveList.getName()); 
      objectiveMap.put(offsetItem.getRemoteId(), offsetItem); 
      System.out.println("This is the remote ID: " + offsetItem.getRemoteId()); 
      mClusterManager.addItem(offsetItem); 
     } 
    } 

InfoWindow의 레이아웃은 괜찮지 만, 내용은 null의 경우 clickedClusterItemclickedCluster은 항상 null입니다 ...

내가 잘못했을 수있는 것에 대한 힌트가 있습니까?

마커와 해당 개체가있는지도를 만드는 데 관련된 몇 가지 답변이 있지만 그 방법을 잘 모르겠습니다.

+0

을 사용 했습니까? –

+0

똑바로 내 build.gradle 파일에서 : 'compile'com.google.maps.android : android-maps-utils : 0.2 + '' –

답변

1

내 앱에서이 작업을 수행했습니다. 너의 안에 이것을 시험해 보라.

GoogleMap googleMap;  
    Double latitude; 
    Double longitude;  
    Marker mark; 
    MarkerOptions marker; 
    Bitmap bitmap; 
    String position, catName, activity; 
    ToggleButton listView, mapView; 
    Hashtable<Integer, String> markers; 

    ArrayList<HomeProperty> list = new ArrayList<HomeProperty>(); 
    ArrayList<HomeProperty> newList; 
    ArrayList<HomeProperty> category = new ArrayList<HomeProperty>(); 
    ArrayList<SearchProperty> searchList = new ArrayList<SearchProperty>(); 
    ArrayList<HomeProperty> locationList = new ArrayList<HomeProperty>(); 

    ProgressDialog dialog; 

    ImageLoader imageLoader; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     // Introduction 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map); 
     initilizeMap(); 
     imageLoader = new ImageLoader(getApplicationContext()); 

     markers = new Hashtable<Integer, String>(); 

     listView = (ToggleButton) findViewById(R.id.listView); 
     mapView = (ToggleButton) findViewById(R.id.mapView); 
     listView.setOnCheckedChangeListener(this); 
     mapView.setOnCheckedChangeListener(this); 
     mapView.setChecked(true); 

     // setting the arraylist containing all data of events 
     ArrayData aData = (ArrayData) getIntent().getSerializableExtra("list"); 
     position = getIntent().getStringExtra("position"); 
     catName = getIntent().getStringExtra("catName"); 
     activity = getIntent().getStringExtra("activity"); 
     list = aData.getList(); 


     ArrayData categoryData = (ArrayData) getIntent().getSerializableExtra(
       "category"); 
     category = categoryData.getList(); 

     if (getIntent().hasExtra("searchData")) { 
      SearchData search = (SearchData) getIntent().getSerializableExtra(
        "searchData"); 
      searchList = search.getList(); 
     } else { 
      searchList = new ArrayList<SearchProperty>(); 
     } 

     newList = new ArrayList<HomeProperty>(); 
     // for all categories 
     if (catName.equalsIgnoreCase("All")) { 
      newList.clear(); 
      newList.addAll(list); 

     } else if (activity.equals("search")) { 
      newList.addAll(list); 

     } 
     // for single category 
     else { 
      for (int count = 0; count < list.size(); count++) { 
       if (list.get(count).getCategoryName().equals(catName)) { 
        newList.add(list.get(count)); 
       } 
      } 
     } 

     // looping through All Transactions 
     for (int count = 0; count < newList.size(); count++) { 
      HomeProperty prop = new HomeProperty(newList.get(count).getLatitude(), 
        newList.get(count).getLongitude()); 

      locationList.add(prop); 
     } 


     for (int count = 0; count < newList.size(); count++) { 
      marker = new MarkerOptions() 
        .position(
          new LatLng(Double.parseDouble(list.get(count) 
            .getLatitude()), Double.parseDouble(newList 
            .get(count).getLongitude()))) 
        .title(newList.get(count).getTitle()) 
        .snippet(
          new JSONMethods().SHORTIMAGEURL 
            + newList.get(count).getImageDirectory() 
            + "/thumb_" + newList.get(count).getImage()); 

      googleMap.addMarker(marker); 

      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(Double.parseDouble(newList.get(count) 
          .getLatitude()), Double.parseDouble(newList.get(count) 
          .getLongitude()))).zoom(12).build(); 

      googleMap.animateCamera(CameraUpdateFactory 
        .newCameraPosition(cameraPosition)); 
      googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter()); 
     } 


     googleMap.getUiSettings().setZoomGesturesEnabled(true); 
     googleMap.getUiSettings().setCompassEnabled(true); 
     googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
     googleMap.getUiSettings().setRotateGesturesEnabled(true); 
    } 

    // set action on home button click in action bar 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     // Handle action bar actions click 
     switch (item.getItemId()) { 
     // case R.id.action_settings: 
     // return true; 
     case android.R.id.home: 
      finish(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

    private class CustomInfoWindowAdapter implements InfoWindowAdapter { 

     private View view; 

     public CustomInfoWindowAdapter() { 
      view = getLayoutInflater().inflate(R.layout.custominfowindow, null); 

     } 

     @Override 
     public View getInfoContents(Marker mark) { 

      if (MapActivity.this.mark != null 
        && MapActivity.this.mark.isInfoWindowShown()) { 
       MapActivity.this.mark.showInfoWindow(); 
      } 
      return view; 
     } 

     @Override 
     public View getInfoWindow(final Marker mark) { 
      MapActivity.this.mark = mark; 

      final ImageView image = ((ImageView) view.findViewById(R.id.badge)); 

      imageLoader.DisplayImage(mark.getSnippet(), image); 

      final String title = mark.getTitle(); 
      final TextView titleUi = ((TextView) view.findViewById(R.id.title)); 
      if (title != null) { 
       titleUi.setText(title); 
      } else { 
       titleUi.setText(""); 
      } 

      return view; 
     } 

    } 
+0

당신의 해결책은 올바른 길로 나를 안내해주었습니다. 감사합니다! –

+0

위대한. 내 대답이 너를 돕는 걸 보니 좋다. –

+0

위대한. 내 대답이 너를 돕는 걸 보니 좋다. –

0

는 다음과 같은 방법을 고려할 수는 나를 위해 일하는 : 어떤 클러스터 러 라이브러리는

public void initilizeMap() { 
    googleMap = mFragment.getMap(); 
    googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
    googleMap.getUiSettings().setZoomControlsEnabled(true); // true to`enter code here` 
    googleMap.getUiSettings().setZoomGesturesEnabled(true); 
    googleMap.getUiSettings().setCompassEnabled(true); 
    googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
    googleMap.getUiSettings().setRotateGesturesEnabled(true); 
    if (googleMap == null) { 
     Toast.makeText(getActivity(), "Sorry! unable to create maps", 
       Toast.LENGTH_SHORT).show(); 
    } 
    mClusterManager = new ClusterManager<MyItem>(getActivity(), googleMap); 
//   googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter()); 
    googleMap.setOnMapLoadedCallback(this); 
    googleMap.setMyLocationEnabled(true); 
    googleMap.setBuildingsEnabled(true); 
    googleMap.getUiSettings().setTiltGesturesEnabled(true); 

    MyItem offsetItem = new MyItem(Double.parseDouble(outletList.get(i).getMap_latitude()), 
             Double.parseDouble(outletList.get(i).getMap_longitude()), title , address); 
     mClusterManager.addItem(offsetItem); 
     googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(offsetItem)); 

} 

private class CustomInfoWindowAdapter implements InfoWindowAdapter { 
    Marker marker; 
    private View view; 
    private MyItem items; 

    public CustomInfoWindowAdapter(MyItem item) { 
    view = getActivity().getLayoutInflater().inflate(
     R.layout.custom_info_window, null); 
    this.items = item; 
    } 

    @Override 
    public View getInfoContents(Marker marker) { 

    if (marker != null && marker.isInfoWindowShown()) { 
     marker.hideInfoWindow(); 
     marker.showInfoWindow(); 
    } 
    return null; 
    } 

    @Override 
    public View getInfoWindow(final Marker marker) { 
    this.marker = marker; 

    String url = null; 

    if (marker.getId() != null && markers != null && markers.size() > 0) { 
     if (markers.get(marker.getId()) != null 
      && markers.get(marker.getId()) != null) { 
     url = markers.get(marker.getId()); 
     } 
    } 

    final ImageView image = ((ImageView) view.findViewById(R.id.badge)); 

    if (url != null && !url.equalsIgnoreCase("null") 
     && !url.equalsIgnoreCase("")) { 
     imageLoader.displayImage(url, image, options, 
      new SimpleImageLoadingListener() { 
      @Override 
      public void onLoadingComplete(String imageUri, View view, 
       Bitmap loadedImage) { 
       super.onLoadingComplete(imageUri, view, loadedImage); 
       getInfoContents(marker); 
      } 
      }); 
    } else { 
     image.setImageResource(R.drawable.ic_launcher); 
    } 

    final String title = items.getTitle(); 
    Log.e(TAG, "TITLE : " + title); 
    final TextView titleUi = ((TextView) view.findViewById(R.id.title)); 
    if (title != null) { 
     titleUi.setText(title); 
    } else { 
     titleUi.setText(""); 
    } 

    final String address = items.getAddress(); 
    final TextView snippetUi = ((TextView) view.findViewById(R.id.snippet)); 
    if (address != null) { 
     snippetUi.setText(address); 
    } else { 
     snippetUi.setText(""); 
    } 
    } 
}