簡體   English   中英

如何為同一個 CustomInfoWindow 設置不同的事件

[英]How can I have different events for the same CustomInfoWindow

所以我知道我的問題有點奇怪,但我會解釋我的意思:所以我有一個 customInfoWindow 和大約 20 個標記。 我已成功添加一個 onclick 偵聽器,錯誤為 0。 問題是我希望以某種方式使該偵聽器針對一個 custominfwindow 標記,而對於另一個 custominfowindow 標記具有不同的事件。 例如,如果您單擊悉尼的 custominfowindow 會顯示吐司,如果您單擊印度的 custominfowindow 它會打開一個布局。 . 我不知道如何為其添加代碼。 有人可以幫我解決這個問題。 我是這個編程的新手,所以我很不理解四分之一的東西,所以請幫我解決這個問題。 以防萬一 MapsActivity 中 onclick 監聽器的代碼:

import androidx.fragment.app.FragmentActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.LinearLayout;
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.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.Map;
//Class starts;

public class MapsActivity extends FragmentActivity implements GoogleMap.OnInfoWindowClickListener,OnMapReadyCallback {

    private GoogleMap mMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);



    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setOnInfoWindowClickListener(this);
@Override
    public void onInfoWindowClick(Marker marker) {
        openIndia();
    }
 public void openIndia(){
        Intent intent = new Intent(this,India.class);
        startActivity(intent);
 }
}

此外,我的代碼更長,但為了節省回答者和我的時間,我只給出了必須更改或有用的代碼。 除此之外,我認為不需要更多代碼。 以防萬一,如果需要請通知我。

一種方法是標記標記:

Marker indiaMarker = mMap.addMarker(new MarkerOptions().position(....).title("India").snippet());
indiaMarker.setTag("India"); // the tag could be anything, an id maybe ?

Marker sydneyMarker = mMap.addMarker(new MarkerOptions().position(....).title("Sydney").snippet());
sydneyMarker.setTag("Sydney");

...

public void onInfoWindowClick(Marker marker) {

        if("India".equals(marker.getTag())) {
            Intent intent = new Intent(this,India.class);
            startActivity(intent);
        } else if("Sydney".equals(marker.getTag())) {
            // showToast() or whatever you want to   
        }
    }

相關文件在這里

public void setTag(對象標簽)
設置標記的標簽。
您可以使用此屬性將任意對象與此標記關聯。 例如,對象可以包含有關標記代表什么的數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM