0

我遇到以下问题:我在 Google 地图上有多个标记。我已经使用一个 For 循环来完成此操作,该循环遍历我的对象数组并为每个对象添加一个标记。标记显示标题和地址。但现在我需要制作可点击的 InfoWindow(或只是可点击的标记),它将显示一个包含附加信息(描述)的 AlertDialog。但我无法让它工作。或者,数据不需要显示在 AlertDialog 中,我也可以显示在 TextView 中。

这是显示标记的部分代码(这是一个 FragmentActivity):

...

Double longitude, latitude;
static LatLng coordinates;
GoogleMap supportMap;
String title, address;
BitmapDescriptor bdf;
ArrayList<GasStations> listGas = new ArrayList<GasStations>();

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

if (listGas != null) {
        for (int i = 0; i < listGas.size(); i++) {
            longitude = listGas.get(i).getLongitude();
            latitude = listGas.get(i).getLatitude();
            naslov = listGas.get(i).getTitle();
            adresa = listGas.get(i).getAddress() + " "
                    + listGas.get(i).getLocation();
            koordinate = new LatLng(latitude, longitude);

            supportMap.addMarker(new MarkerOptions().position(koordinate)
                    .title(title).snippet(address).icon(bdf));
            supportMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    coordinates, 10));
        }
    }

标记显示得很好,它们的 InfoWindows 显示正确的数据。但现在我想根据单击的 InfoWindow 显示其他信息。如果这不能通过 InfoWindow 完成,是否可以通过单击特定标记来完成?

4

2 回答 2

1

您可以创建自定义信息窗口

GoogleMap googleMap;

    Map.setInfoWindowAdapter(new InfoWindowAdapter() {


        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }


        @Override
        public View getInfoContents(Marker arg0) {

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

            // Getting the position from the marker
            LatLng latLng = arg0.getPosition();


            TextView tvLat = (TextView) v.findViewById(R.id.lat);


            TextView tvLng = (TextView) v.findViewById(R.id.lng);

            tvLat.setText("Lat:" + latLng.latitude);


            return v;

        }
    });
于 2013-07-02T10:27:19.683 回答
-1

公共类 MainActivity 扩展 FragmentActivity {

GoogleMap googleMap;
String add;

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    googleMap = mapFragment.getMap();
    googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker arg0) {

            View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
            LatLng latLng = arg0.getPosition();
            TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
            TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
            TextView loc = (TextView) v.findViewById(R.id.loc);
            Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
            try {           
                List<Address> addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
                if(addresses.size() > 0)
                add = addresses.get(0).getAddressLine(0);
            }

            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
             }
            loc.setText(""+add);
            tvLat.setText("" + latLng.latitude);
            tvLng.setText(""+ latLng.longitude);

            return v;

        }
    });

    googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {
            googleMap.clear();

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(arg0);
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0));

            Marker marker = googleMap.addMarker(markerOptions);
            marker.showInfoWindow();

        }
    });

    googleMap.setOnInfoWindowClickListener(
            new OnInfoWindowClickListener() {

                @Override
                public void onInfoWindowClick(Marker marker) {
                    Toast.makeText(getBaseContext(), "Info Window Clicked@" + marker.getId(), 
                            Toast.LENGTH_SHORT).show();
                }
            });

}

@Override
protected void onResume() {
 super.onResume();

 int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
 if (resultCode == ConnectionResult.SUCCESS) {       
  Toast.makeText(getApplicationContext(), "isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_SHORT).show();
 }
 else {      
  GooglePlayServicesUtil.getErrorDialog(resultCode, this, 1);
  Toast.makeText(getApplicationContext(), "isGooglePlayServicesAvailable ERROR", Toast.LENGTH_SHORT).show();
 }

}

}

于 2013-07-02T11:41:10.180 回答