Google使用两个代码段映射v2标记

时间:2013-03-18 09:21:51

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

下午好。是否可以添加一个标记的几个片段?适用于Google地图的Android版本2

 Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(MELBOURNE)
                          .title("Melbourne")
                          .snippet("Population: 4,137,400"));

这是我的信息窗口

class MyInfoWindowAdapter implements InfoWindowAdapter{

           private final View myContentsView;

               MyInfoWindowAdapter(){
               LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);

               myContentsView = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
               myContentsView.setLayoutParams(lp);
               }

       public View getInfoContents(Marker marker) {

               tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
               tvTitle.setText(marker.getTitle());
               tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
               tvSnippet.setText(marker.getSnippet());

           return myContentsView;
       }

       public View getInfoWindow(Marker marker) {
              // TODO Auto-generated method stub
              return null;
       }

   }

我想在必要时显示不同片段的不同标记。我该怎么做呢?它有可能吗?

更新 您可能没有理解或错误地解释过。

public void ParseQueryMap() {
          ParseQuery query = new ParseQuery("MyObject");
          query.findInBackground(new FindCallback() {
          public void done(List<ParseObject> myObject, ParseException e) {
          if (e == null) {

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

                          commGet =  myObject.get(i).getString("Comment");
                          bugGet = myObject.get(i).getObjectId();   

                          geo1Dub = myObject.get(i).getParseGeoPoint("location").getLatitude();
                          geo2Dub = myObject.get(i).getParseGeoPoint("location").getLongitude();

                         Location aLocation = new Location("first");
                         aLocation.setLatitude(geo1Dub);
                         aLocation.setLongitude(geo2Dub);
                         Location bLocation = new Location("second");
                         bLocation.setLatitude(location.getLatitude());
                         bLocation.setLongitude(location.getLongitude());
                         int distance = (int)aLocation.distanceTo(bLocation);

                              if (distance<rad) {
                                  myMap.addMarker(new MarkerOptions().position(new LatLng(geo1Dub,geo2Dub)).title(commGet).snippet(snippet)
                                      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));     

                               } else {

                               }                                                      

                         }

             } else {
                    Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_SHORT).show();
              }
          }
      });

我想为每个标记获取bugGet,用户bug不会显示,但是当她点击infowindow时我可以学习bugGet特定标记。 “bugGet”它“id”我数据库中的每个标记。用户不需要它,我需要你。

2 个答案:

答案 0 :(得分:0)

以下是示例,您可以检查标记的坐标,然后确定要显示的infoWindow

 map.setInfoWindowAdapter(new InfoWindowAdapter() {

            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker args) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker args) {

                // Getting view from the layout file info_window_layout
                View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);

                // Getting the position from the marker
                clickMarkerLatLng = args.getPosition();

                TextView title = (TextView) v.findViewById(R.id.tvTitle);
                title.setText(args.getTitle());

                map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {          
                    public void onInfoWindowClick(Marker marker) 
                    {
                        if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
                        {   
                            if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
                                    String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                            {
                                Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.",  Toast.LENGTH_SHORT).show();
                            }
                            else
                            {
                                FlurryAgent.onEvent("Start navigation window was clicked from daily map");
                                tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
                                for (Task tmptask : tasksRepository)
                                {
                                    String tempTaskLat = String.valueOf(tmptask.getLatitude());
                                    String tempTaskLng = String.valueOf(tmptask.getLongtitude());

                                    Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));

                                    if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                                    {  
                                        task = tmptask;
                                        break;
                                    }
                                }

                                Intent intent = new Intent(getApplicationContext() ,RoadDirectionsActivity.class);
                                intent.putExtra(TasksListActivity.KEY_ID, task.getId());
                                startActivity(intent);

                            }
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.",  Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                // Returning the view containing InfoWindow contents
                return v;

            }
        });  

答案 1 :(得分:0)

在我的一个项目中,我创建了一个新类MarkerSnippet并将所有信息添加到此类中,例如:

public class MarkerSnippet {
  private String foo;
  private String bar;
  public MarkerSnippet(String foo, String bar) {
    this.foo = foo;
    this.bar = bar;
  }
  // getter and setter for foo and bar
}

然后我为每个标记创建一个MarkerSnippet实例,并使用GSON将其添加为JSON字符串(因为该代码段仅接受字符串):

Gson gson = new Gson(); // remark: only one Gson instane is needed
String fooContent = "myFoo";
String barContent = "myBar";

String snippetString = gson.toJson(new MarkerSnippet(fooContent, barContent));
map.addMarker(
  new MarkerOptions().position(position)
    .title(title)
    .snippet(snippetString)
);

然后在InfoWindowAdapter中,您需要将JSON字符串转换为MarkerSnippet,并仅将此部分代码段添加到您要显示的视图中。