通过InfoWindow将数据发送到另一个Android活动

时间:2017-03-22 11:51:30

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

我有一个for循环,用于填充地图上的标记。 for循环还包含OnInfoWindowClickListener,以便在单击InfoWindow时打开一个活动。此活动应根据选择的InfoWindow显示详细信息。现在,InfoWindow会显示不同标记的不同详细信息,但单击不同InfoWindows时打开的活动每次都会显示相同的信息。

for循环:

for (final infoToStore details : info) {
        marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
        .title(details.getName())
        .snippet(details.getDesc()));

        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Intent intent = new Intent(MainActivity.this, InfoWindow.class);
                intent.putExtra(NAME, details.getName());
                intent.putExtra(DESCRIPTION, details.getDesc());
                intent.putExtra(CATEGORY, details.getCat());
                intent.putExtra(IMAGE_URL, details.getUrl());
                intent.putExtra(PUSH_ID, details.getPushID());
                startActivity(intent);
                startActivity(new Intent(MainActivity.this, InfoWindow.class));
            }
        });
    }
}

选择InfoWindow时打开的活动代码的一部分:

name = (TextView) findViewById(R.id.name);
    desc = (TextView) findViewById(R.id.desc);
    cat = (TextView) findViewById(R.id.cat);
    image = (ImageView) findViewById(R.id.image);
    report = (Button) findViewById(R.id.report);

    String nameString = returnValueFromBundles (MainActivity.NAME);
    String descString = returnValueFromBundles (MainActivity.DESCRIPTION);
    String catString = returnValueFromBundles (MainActivity.CATEGORY);
    String URLString = returnValueFromBundles (MainActivity.IMAGE_URL);
    final String pushID = returnValueFromBundles(MainActivity.PUSH_ID);

    name.setText(nameString);
    desc.setText(descString);
    cat.setText(catString);


    Picasso.with(getApplicationContext())
            .load(URLString)
            .into(image);

    report.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent email = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailTo","jolan.goburdhun@uom.umail.ac.mu",null));
            email.putExtra(Intent.EXTRA_SUBJECT, "Reporting a service");
            email.putExtra(Intent.EXTRA_TEXT, pushID);
            startActivity(Intent.createChooser(email, "Choose an email client"));
        }
    });

}

private String returnValueFromBundles(String key) {
    Bundle inBundle = getIntent().getExtras();
    String returnedValue = inBundle.get(key).toString();
    return returnedValue;
}

2 个答案:

答案 0 :(得分:2)

尝试这种方式,如果您的bean(InfoWindow Click)具有不同的值,您将在details获得不同的值。

 for (final infoToStore details : info) {
        marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
                .title(details.getName())
                .snippet(details.getDesc()));
         // Attach your bean with marker
        marker.setTag(details);

        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                //Get Attached bean from marker
                infoToStore infoAttached = ((infoToStore) marker.getTag());

                Intent intent = new Intent(MainActivity.this, InfoWindow.class);
                intent.putExtra(NAME, infoAttached.getName());
                intent.putExtra(DESCRIPTION, infoAttached.getDesc());
                intent.putExtra(CATEGORY, infoAttached.getCat());
                intent.putExtra(IMAGE_URL, infoAttached.getUrl());
                intent.putExtra(PUSH_ID, infoAttached.getPushID());
                startActivity(intent);

            }
        });
    }

答案 1 :(得分:0)

首先要做的是 - 从mMap.setOnInfoWindowClickListener()循环中删除for,不需要这样做。相反,您可以将其设置在for loop

之外

你的问题是 -

startActivity(intent);
startActivity(new Intent(MainActivity.this, InfoWindow.class));

你为什么两次开始活动?只需删除第二行就可以了; - )