单击标记时,谷歌标记打开活动

时间:2017-12-04 23:16:54

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

我创建了一个应用程序,它通过从firebase上的数据库获取的地图上的标记显示条形位置列表。

每个酒吧都有一份饮料清单。

我希望发生的是,当点击一个标记时,将打开一个新活动,并在新活动的顶部选择一个栏的名称

我希望发生的是,当点击标记时,它将打开一个新活动

//场地宣言清单

List<Venue> venueList;

// onCreate方法

    venueList = new ArrayList<>();
    mVenues.push().setValue(marker);

// onMapsReady方法,让场地进入地图

  public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mVenues.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot s : dataSnapshot.getChildren()){
                Venue venue = s.getValue(Venue.class);

                venueList.add(venue);
                for (int i = 0; i < venueList.size(); i++)
                {
                    LatLng latLng = new LatLng(venue.venueLat,venue.venueLong);
                    if (mMap != null) {
                        marker = mMap.addMarker(new MarkerOptions()
                                .position(latLng).title(venue.venueName));
                                           }
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });

//标记点击事件

  //CMarker click event to take user to purchase beverage
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener(){
        public boolean onMarkerClick(Marker marker) {
            String title = marker.getTitle();
            {
                for (int i = 0; i < venueList.size();) {
                    //getting the selected venue
                    Venue venue = venueList.get(i);
                    //creating an intent

                    Intent intent = new Intent(getApplicationContext(), viewbeverageActivity.class);

                    //putting venue name and id to intent
                    intent.putExtra(VENUE_ID, venue.getVenueId());
                    intent.putExtra(VENUE_NAME, venue.getVenueName());

                    //starting the activity with intent
                    startActivity(intent);

                }
                return false;
            }
        }
    });

//场地等级

package uk.ac.ferry_j2ulster.beerapp;

import com.google.firebase.database.IgnoreExtraProperties;

public class Venue {
    private String venueId;
    public String venueName;
    private String venueType;
    public double venueLat;
    public double venueLong;


    public Venue(){

    }

    public Venue(String venueId, String venueName, String venueType,double venueLat, double venueLong) {
        this.venueId = venueId;
        this.venueName = venueName;
        this.venueType = venueType;
        this.venueLat =  venueLat;
        this.venueLong =  venueLong;


    }

    public String getVenueId() {
        return venueId;
    }

    public String getVenueName() {
        return venueName;
    }

    public String getVenueType() {
        return venueType;
    }

    public double getVenueLong() { return venueLong;
    }

    public double getVenueLat() {return venueLat;
    }
}

我希望使用在顶部选为标题的栏开启一项新活动

没有错误,不知道如何从这个开始

2 个答案:

答案 0 :(得分:1)

问题是你使用循环来遍历每个场地,并为所有场地调用startActivity()

相反,定义一个将每个标记ID映射到场地ID的HashMap:

 Map<String, String> mMarkerMap = new HashMap<>();

然后,在每个Marker的HashMap中输入一个条目:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot s : dataSnapshot.getChildren()){
        Venue venue = s.getValue(Venue.class);

        venueList.add(venue);
        for (int i = 0; i < venueList.size(); i++)
        {
            LatLng latLng = new LatLng(venue.venueLat,venue.venueLong);
            if (mMap != null) {
                marker = mMap.addMarker(new MarkerOptions()
                        .position(latLng).title(venue.venueName));
                //Added:
                mMarkerMap.put(marker.getId(), venue.getVenueId());
            }
        }
    }
}

然后,当用户点击标记时,从Marker / HashMap获取标题和Venue ID,并将其发送到新活动:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //...........

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            String venueID = mMarkerMap.get(marker.getId());
            String venueName = marker.getTitle();
            Intent intent = new Intent(MapActivity.this, NewActivity.class);
            intent.putExtra(VENUE_NAME, venueName);
            intent.putExtra(VENUE_ID, venueID);
            startActivity(intent);

            return false;
        }
    });
}

在新活动中,只需输入标题和Venue ID字符串:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);

    String venueName = getIntent().getStringExtra(MapActivity.VENUE_NAME);
    if (title != null) {
        getSupportActionBar().setTitle(title);
    }

    String venueID = getIntent().getStringExtra(MapActivity.VENUE_ID);
    if (venueID != null) {
        //use venue ID to get the information about this venue
    }

    //.......
 }

答案 1 :(得分:0)

您还需要将场地信息传递给活动:

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

//...........

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        String title = marker.getTitle();
        Venue venue = NULL;
        for(int i=0;i<venueList.size();i++)
        {
          if(venueList.get(i).getTitle().equal(title))
             venue = venueList.get(i);
        }
        Intent intent = new Intent(MapActivity.this, NewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("venue",venue.toJSON());
        startActivity(intent);

        return false;
    }
});

}

在您的场地类中设置 toJSON()功能,以及 fromJSON(),这也有助于您发送场地数据。