2017-02-20 85 views
0

我有我的以下代码如下,我把2标记只是为了测试目的到我的代码。问题一切正常,现在标记出现问题现在是进一步定制,以便我可以控制显示所有信息窗口或关闭所有信息窗口。安卓谷歌地图标记,使所有自定义infowindow显示

protected void onPostExecute(String result) { 
      String s = result.trim(); 


      //Toast.makeText(getApplicationContext(), "restult is"+s, Toast.LENGTH_LONG).show(); 
      String stringSucess = ""; 
      //markers = new Hashtable<String, String>(); 
      int height = 50; 
      int width = 40; 
      BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.drawable.basgray); 
      Bitmap b=bitmapdraw.getBitmap(); 
      Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false); 
      LatLng sydney = new LatLng(-34, 151); 

      Marker mk = mMap.addMarker(new MarkerOptions() 
        .position(new LatLng(Double.parseDouble("-34"),Double.parseDouble("151"))) 
        .title("First") 
        .snippet("") 
        .icon(BitmapDescriptorFactory.fromBitmap(smallMarker))); 
      mk.showInfoWindow(); 

      //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromBitmap(smallMarker))).showInfoWindow(); 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 



      markers.put(mk.getId(), "RED"); 

      Marker mk2 = mMap.addMarker(new MarkerOptions() 
        .position(new LatLng(Double.parseDouble("-10"),Double.parseDouble("151"))) 
        .title("Second") 
        .snippet("") 
        .icon(BitmapDescriptorFactory.fromBitmap(smallMarker))); 
      mk2.showInfoWindow(); 
      //setListAdapter(adapter); 
      markers.put(mk2.getId(), "Green"); 
     } 

在我的另一个功能,我已经设置为自定义信息窗口如下。在这里我已经设置了信息窗口将根据下面声明的另一个类来定制。

public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     //mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(null))); 
     mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter()); 

    } 

这是我的自定义功能来控制我的信息窗口的颜色和图标。

private class CustomInfoWindowAdapter implements InfoWindowAdapter { 

      private View view; 

      public CustomInfoWindowAdapter() { 
       //view = getLayoutInflater(null).inflate(R.layout.custom_info_window, 
       //  null); 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 

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

      @Override 
      public View getInfoWindow(final Marker marker) { 
       //MapActivityFragment1.this.marker = marker; 
       view = getLayoutInflater(null).inflate(R.layout.custom_info_window, 
         null); 
       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 String title = marker.getTitle(); 
       final TextView titleUi = ((TextView) view.findViewById(R.id.title)); 
       if (title != null) { 
        titleUi.setText(title); 
       } else { 
        titleUi.setText(""); 
       } 

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

       return view; 
      } 
     } 

回答

2

为什么不保存创建标记的实例?可能在列表中,并调用一个函数来显示标记(marker.showInfoWindow())上的信息窗口和隐藏它们的相反方向。

代码片段:创建一个全局变量来保存标记列表。

ArrayList<Marker> markersList = new ArrayList<>(); 

现在当您根据您的代码创建标记时,将它们添加到列表中。

Marker mk = mMap.addMarker(new MarkerOptions() 
       .position(new LatLng(Double.parseDouble("-34"),Double.parseDouble("151"))) 
       .title("First") 
       .snippet("") 
       .icon(BitmapDescriptorFactory.fromBitmap(smallMarker))); 
markersList.add(mk); 

现在一个简单的函数会做

private void showOrHideInfoWindows(boolean shouldShow){ 
    for(Marker marker:markersList){ 
     if(shouldShow) 
      marker.showInfoWindow(); 
     else 
      marker.hideInfoWindow(); 
    } 
} 

使用信息窗口的一个好方法是(在地图片段)

View view = getLayoutInflater().inflate(R.layout.custom_info_window, null); 
mMap.setInfoWindowAdapter(new CustomInfoWindow(view, this)); 
mMap.setOnInfoWindowClickListener(this); 

和自定义类是像

public class CustomInfoWindow implements GoogleMap.InfoWindowAdapter { 

View infoWindow; 
IMapDelegate mapDelegate; 

public CustomInfoWindow(View view, IMapDelegate mapDelegate) { 
    infoWindow = view; 
    this.mapDelegate = mapDelegate; 
} 

@Override 
public View getInfoContents(Marker marker) { 
    displayView(marker); 
    mapDelegate.onInfoWindowShown(infoWindow); 
    return infoWindow; 
} 

private void displayView(final Marker marker) { 
    ((TextView)infoWindow.findViewById(R.id.markerTextView)).setText(marker.getTitle()); 
} 

@Override 
public View getInfoWindow(Marker marker) { 
    return null; 
} 

} 
+0

任何想法如何拯救他们我很新,其实几乎没有设法得到这个定制的代码现在正在努力与下一个任务。任何代码片段如何实现你的想法请吗? – user5313398

+0

infact我将标记的id保存到散列表中我可以利用它吗?但是,在哪里打电话给这个节目和关闭信息窗口? – user5313398

+0

只有当我们点击相同的标记图标时,标记的ID才能再次使用。我们使用当前标记回调onMarkerClick。我们可以检查它的id与我们的hasmap中保存的id。 – Vansi