2

单击地图中的自定义标记后,我试图通过 Google Maps Android API v2 显示来自 Google Maps 的自定义对话框。自定义标记将显示其放置位置的当前交通状况。自定义对话框包含地点的名称、当前道路的南北行交通状况、一些建议(如道路正在建设中等)以及交通更新的时间。所有这些数据都来自一个 XML 文件并存储在 sqlite 数据库中。我现在的问题是,当我单击任何标记时,它只显示查询中第一个数据的对话框。我知道我的查询是正确的,因为标记包含有关交通状况的正确图标。我想问题只出在循环过程上。我也尝试使用 for 循环,但生成了相同的输出。谁能帮助我实现我的目标?任何帮助或建议将不胜感激。提前致谢。

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapViewActivity extends android.support.v4.app.FragmentActivity {

    public static final LatLng CITYHALL = new LatLng(07.0644444, 125.6077778);
    private GoogleMap mMap;

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

        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {

        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private Boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null && ni.isConnected())
            return true;

        return false;
    }


    private void setUpMap() {

        if (isOnline()) {

            String query = "SELECT * FROM TrafficCondition";
            SQLiteDatabase db = openOrCreateDatabase("_dds", MODE_PRIVATE, null);
            final Cursor c = db.rawQuery(query, null);

            final Dialog d = new Dialog(MapViewActivity.this);
            d.setContentView(R.layout.mapview_marker_dialog);
            d.setTitle("Traffic Information");

            if (c.moveToFirst()) {
                do {
                    final int id = c.getInt(c.getColumnIndex("id"));
                    final String name = c.getString(c.getColumnIndex("name"));
                    final double lat = c.getDouble(c.getColumnIndex("lat"));
                    final double lng = c.getDouble(c.getColumnIndex("lng"));
                    final int north_condition = c.getInt(c
                            .getColumnIndex("north_condition"));
                    final int south_condition = c.getInt(c
                            .getColumnIndex("south_condition"));
                    final String north_advisory = c.getString(c
                            .getColumnIndex("north_advisory"));
                    final String south_advisory = c.getString(c
                            .getColumnIndex("south_advisory"));
                    final String icon = c.getString(c.getColumnIndex("icon"));
                    final String traffic_last_updated = c.getString(c
                            .getColumnIndex("traffic_last_updated"));

                    final SimpleDateFormat dateFormat = new SimpleDateFormat(
                            "EEE, dd MMM yyyy kk:mm:ss Z");
                    final Date app_last_updated = new Date(c.getLong(c
                            .getColumnIndex("app_last_updated")));
                    final String appLastUpdated = dateFormat
                            .format(app_last_updated);

                    mMap = ((SupportMapFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.map)).getMap();

                    mMap.addMarker(new MarkerOptions()
                            .position(new LatLng(lat, lng))
                            .icon(BitmapDescriptorFactory.fromAsset(new String(
                                    icon + ".png"))));


                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(CITYHALL,
                            12));

                    mMap.setOnMarkerClickListener(new OnMarkerClickListener() {

                        @Override
                        public boolean onMarkerClick(Marker marker) {
                            // TODO Auto-generated method stub

                            String nNoData = "NorthBound: No Data";
                            String nLightCon = "NorthBound: Light";
                            String nModerateCon = "NorthBound: Moderate";
                            String nHeavyCon = "NorthBound: Heavy";

                            String sNoData = "SouthBound: No Data";
                            String sLightCon = "SouthBound: Light";
                            String sModerateCon = "SouthBound: Moderate";
                            String sHeavyCon = "SouthBound: Heavy";

                            TextView title = (TextView) d
                                    .findViewById(R.id.traffic_con_node_title);
                            title.setText(name);

                            TextView nTrafficCon = (TextView) d
                                    .findViewById(R.id.n_traffic_con);
                            TextView sTrafficCon = (TextView) d
                                    .findViewById(R.id.s_traffic_con);

                            TextView nAdvisoryTitle = (TextView) d
                                    .findViewById(R.id.n_traffic_con_ad_title);
                            TextView sAdvisoryTitle = (TextView) d
                                    .findViewById(R.id.s_traffic_con_ad_title);

                            TextView nTrafficConAd = (TextView) d
                                    .findViewById(R.id.n_traffic_con_ad_content);
                            TextView sTrafficConAd = (TextView) d
                                    .findViewById(R.id.s_traffic_con_ad_content);

                            TextView TrafficLastUpdated = (TextView) d
                                    .findViewById(R.id.traffic_con_last_updated);

                            if (north_condition == 0) {
                                nTrafficCon
                                        .setBackgroundResource(R.drawable.n_bg_nodata);
                                nTrafficCon.setText(nNoData);
                            } else if (north_condition == 1) {
                                nTrafficCon
                                        .setBackgroundResource(R.drawable.n_bg_light);
                                nTrafficCon.setText(nLightCon);
                            } else if (north_condition == 2) {
                                nTrafficCon
                                        .setBackgroundResource(R.drawable.n_bg_moderate);
                                nTrafficCon.setText(nModerateCon);
                            } else if (north_condition == 3) {
                                nTrafficCon
                                        .setBackgroundResource(R.drawable.n_bg_heavy);
                                nTrafficCon.setText(nHeavyCon);
                            }

                            if (south_condition == 0) {
                                sTrafficCon
                                        .setBackgroundResource(R.drawable.s_bg_nodata);
                                sTrafficCon.setText(sNoData);
                            } else if (south_condition == 1) {
                                sTrafficCon
                                        .setBackgroundResource(R.drawable.s_bg_light);
                                sTrafficCon.setText(sLightCon);
                            } else if (south_condition == 2) {
                                sTrafficCon
                                        .setBackgroundResource(R.drawable.s_bg_moderate);
                                sTrafficCon.setText(sModerateCon);
                            } else if (south_condition == 3) {
                                sTrafficCon
                                        .setBackgroundResource(R.drawable.s_bg_heavy);
                                sTrafficCon.setText(sHeavyCon);
                            }

                            String empty = "null";

                            if ((north_advisory.trim().length() > 0)
                                    && (!north_advisory.equals(empty))) {
                                nAdvisoryTitle.setVisibility(View.VISIBLE);
                                nTrafficConAd.setVisibility(View.VISIBLE);
                                nTrafficConAd.setText(north_advisory);
                            } else if (nAdvisoryTitle.equals(null)) {
                                nAdvisoryTitle.setVisibility(View.GONE);
                                nTrafficConAd.setVisibility(View.GONE);
                            }

                            if ((south_advisory.trim().length() > 0)
                                    && (!south_advisory.equals(empty))) {
                                sAdvisoryTitle.setVisibility(View.VISIBLE);
                                sTrafficConAd.setVisibility(View.VISIBLE);
                                sTrafficConAd.setText(south_advisory);
                            } else {
                                sAdvisoryTitle.setVisibility(View.GONE);
                                sTrafficConAd.setVisibility(View.GONE);
                            }

                            TrafficLastUpdated.setText("Updated "
                                    + traffic_last_updated + ".");

                            d.show();
                            return false;
                        }
                    });
                } while (c.moveToNext());
            }
        } else {
            Toast.makeText(MapViewActivity.this, "No connection..",
                    Toast.LENGTH_LONG).show();
        }
    }
}
4

1 回答 1

0

您为每个数据元素调用一次 mMap.setOnMarkerClickListener。你应该只调用一次。您应该将数据库中的数据存储在由标记 ID 索引的 Map 中。然后,当用户点击标记时,您可以检索它并显示适当的信息。

于 2013-08-23T21:16:18.917 回答