简体   繁体   English

如何迭代在 ViewRestaurantList 中创建的 ArrayList 以在 map 上放置标记?

[英]How do I iterate the ArrayList created in ViewRestaurantList to place markers on the map?

Restaurant Class餐厅 Class

package sg.edu.rp.c345.a01.hungrycomehere;

import android.os.Bundle;

import com.google.android.maps.GeoPoint;

public class Restaurant {

    // Declarations Fields
    String name;
    String address;
    String phoneNumber;
    String email;
    String url;
    String openingHours;
    String closingHours;
    GeoPoint latLong;

    // Constructor generated from Fields 
    public Restaurant(String name, String address, String phoneNumber,
            String email, String url, String openingHours, String closingHours,
            GeoPoint latLong) {
        super();
        this.name = name;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.email = email;
        this.url = url;
        this.openingHours = openingHours;
        this.closingHours = closingHours;
        this.latLong = latLong;
    }

    // A mapping from String values to various Parcelable types.
    // Construct from Bundle
    public Restaurant(Bundle b) {
        name = b.getString("restaurantName");
        address = b.getString("restaurantAddress");
        phoneNumber = b.getString("restaurantPhoneNumber");
        email = b.getString("restaurantEmail");
        url = b.getString("restaurantUrl");
        openingHours = b.getString("restaurantOpeningHrs");
        closingHours = b.getString("restaurantClosingHrs");
        latLong = new GeoPoint(b.getInt("restaurantLat"), b.getInt("restaurantLng"));
    }

    // Convert into bundle
    public Bundle getBundle() {
        Bundle bundle = new Bundle();
        bundle.putString("restaurantName", name);
        bundle.putString("restaurantAddress", address);
        bundle.putString("restaurantUrl", url);
        bundle.putString("restaurantEmail", email);
        bundle.putString("restaurantPhoneNumber", phoneNumber);
        bundle.putString("restaurantOpeningHrs", openingHours);
        bundle.putString("restaurantClosingHrs", closingHours);
        bundle.putInt("restaurantLat", latLong.getLatitudeE6());
        bundle.putInt("restaurantLng", latLong.getLongitudeE6());
        return bundle;
    }

    // Get, Set, toString!
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getOpeningHours() {
        return openingHours;
    }

    public void setOpeningHours(String openingHours) {
        this.openingHours = openingHours;
    }

    public String getClosingHours() {
        return closingHours;
    }

    public void setClosingHours(String closingHours) {
        this.closingHours = closingHours;
    }

    public GeoPoint getLatLong() {
        return latLong;
    }

    public void setLatLong(GeoPoint latLong) {
        this.latLong = latLong;
    }

    @Override
    public String toString() {
        return "Restaurant [name=" + name + ", address=" + address
                + ", phoneNumber=" + phoneNumber + ", email=" + email
                + ", url=" + url + ", openingHours=" + openingHours
                + ", closingHours=" + closingHours + ", latLong=" + latLong
                + "]";
    }

}

Restaurant List餐厅名单

package sg.edu.rp.c345.a01.hungrycomehere;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.android.maps.GeoPoint;

public class ViewRestaurantList extends ListActivity {
    // Declarations
    ListView restaurantListView;
    ArrayList<Restaurant> restaurantList = new ArrayList<Restaurant>();
    ArrayList<String> restaurantName = new ArrayList<String>();
    ArrayAdapter<String> restaurantAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restaurant_list_view);
        setRestaurant("Ban Leong Wah Hoe Seafood",
                "122 Casuarina Road, Singapore", "64522824",
                "contact@wahhoe-seafood.com", "http://www.wahhoe-seafood.com",
                "5:00 PM", "1:30 AM", new GeoPoint(1376700, 103828170));
        setRestaurant(
                "Ngee Fou Restaurant (Hakka) Ampang Yong Tou Foo",
                "928 Upper Thomson Road, Singapore",
                "64521801",
                null,
                "http://www.hungrygowhere.com/singapore/ngee_fou_restaurant_hakka_ampang_yong_tou_foo/",
                "9:30 AM", "7:30 PM", new GeoPoint(1399455, 103817878));
        setElements();
    }

    // Create onListItemClick method
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Intent viewRestaurant = new Intent(getBaseContext(),
                ViewRestaurantMap.class);
        viewRestaurant.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Put all restaurants' data across activities.
        viewRestaurant.putExtras(restaurantList.get(position).getBundle());
        startActivity(viewRestaurant);
    }

    // Populate list
    private void setElements() {
        restaurantListView = getListView();
        registerForContextMenu(restaurantListView);
        restaurantAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, restaurantName);
        restaurantListView.setAdapter(restaurantAdapter);
    }

    private void setRestaurant(String name, String address, String phoneNumber,
            String email, String url, String openingHours, String closingHours,
            GeoPoint latLong) {
        restaurantList.add(new Restaurant(name, address, phoneNumber, email,
                url, openingHours, closingHours, latLong));
        restaurantName.add(name);

    }
}

Restaurant Map餐厅 Map

package sg.edu.rp.c345.a01.hungrycomehere;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class ViewRestaurantMap extends MapActivity {

    // Declarations
    static final private int MAP_VIEW = Menu.FIRST;
    static final private int SAT_VIEW = Menu.FIRST + 1;
    MapView map;
    MapController myMapController;
    MyLocationOverlay myLocationOverlay;
    List<Overlay> mapOverlays;
    Drawable marker;
    HelloItemizedOverlay itemizedOverlay;
    Bundle restaurants;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restaurant_map);

        // Get Intents from ViewShoeList and Views from layout
        restaurants = new Bundle(getIntent().getExtras());

        // MapView and Controller
        map = (MapView) findViewById(R.id.map);
        map.setBuiltInZoomControls(true);
        myMapController = map.getController();
        myMapController.setZoom(11);
        myMapController.setCenter(new GeoPoint(1352083, 103819836));

        // MyLocationOverlay
        myLocationOverlay = new MyLocationOverlay(this, map);
        myLocationOverlay.enableMyLocation();
        map.getOverlays().add(myLocationOverlay);

        // Get Overlays
        mapOverlays = map.getOverlays();
        marker = this.getResources().getDrawable(R.drawable.marker);
        itemizedOverlay = new HelloItemizedOverlay(marker);

        // Add Markers on Restaurants
        for (int i = 0; i < restaurants.size(); i++) {

        }
    }

    // Create Options menu
    @Override
    public boolean onCreateOptionsMenu(Menu optionsMenu) {
        super.onCreateOptionsMenu(optionsMenu);
        // Create and add new menu items.
        MenuItem itemMap = optionsMenu.add(0, MAP_VIEW, Menu.NONE,
                R.string.mapview);
        MenuItem itemSatellite = optionsMenu.add(0, SAT_VIEW, Menu.NONE,
                R.string.satview);

        itemMap.setIcon(R.drawable.mapview);
        itemSatellite.setIcon(R.drawable.satelliteview);
        return true;
    }

    // Execute option's action based on user click.
    @Override
    public boolean onOptionsItemSelected(MenuItem optionItem) {
        super.onOptionsItemSelected(optionItem);
        switch (optionItem.getItemId()) {
        case (MAP_VIEW): {

            map.setSatellite(false);
            return true;
        }
        case (SAT_VIEW): {

            map.setSatellite(true);
            return true;
        }

        }
        return false;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

You can create a method in your ItemizedOverlay class to add all restaurant objects.您可以在 ItemizedOverlay class 中创建一个方法来添加所有餐厅对象。 For this, your ItemizedOverlay will need to have an internal ArrayList bound to the overlay by overriding the createItem method.为此,您的 ItemizedOverlay 将需要通过覆盖 createItem 方法将内部 ArrayList 绑定到叠加层。 It might also help to subclass OverlayItem for your restaurant map items.为您的餐厅 map 项目子类化 OverlayItem 也可能会有所帮助。 Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM