从Google Maps Android点击标记时如何显示对话框

时间:2013-02-10 04:35:44

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

点击地图中的自定义标记后,我会尝试通过Google Maps Android API v2从Google地图中显示自定义对话框。自定义标记将在其放置位置显示当前的交通状况。自定义对话框包含地点的名称,当前的道路北行和南行交通状况,一些建议(如道路正在建设中等),以及交通更新的时间。所有这些数据都来自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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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