Java 标记id在设置标记实例时更改

Java 标记id在设置标记实例时更改,java,android,google-maps,geolocation,google-maps-markers,Java,Android,Google Maps,Geolocation,Google Maps Markers,我正在使用谷歌地图和标记来显示一些关于照片拍摄地点的信息和其他相关信息 所以基本上我有一个活动,我有一个与特定植物相关的照片列表,在活动中我可以选择在地图上看到它。当我按下该选项时,所有照片(有位置)在地图上显示为绿色标记,当前单击的照片显示为黄色标记 因此,当我点击“查看地图”时,我会传递我所有照片的数组、选定的照片id以及用户的当前位置 像这样: public void showMap(View view){ Intent i = new Intent(this,MapsA

我正在使用谷歌地图和标记来显示一些关于照片拍摄地点的信息和其他相关信息

所以基本上我有一个活动,我有一个与特定植物相关的照片列表,在活动中我可以选择在地图上看到它。当我按下该选项时,所有照片(有位置)在地图上显示为绿色标记,当前单击的照片显示为黄色标记

因此,当我点击“查看地图”时,我会传递我所有照片的数组、选定的照片id以及用户的当前位置

像这样:

 public void showMap(View view){
        Intent i = new Intent(this,MapsActivity.class);
        i.putExtra("currentLat",location.getLatitude());
        i.putExtra("currentLon",location.getLongitude());
        i.putExtra("positionCurr",pos);
        Bundle bundle = new Bundle();
        bundle.putSerializable("key", photos);
        i.putExtras(bundle);
        startActivity(i);
    }
public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        Marker marker;
        LatLng currentZone = null;
        LatLng plantLocation = null;
        LatLng photosLocation = null;
        positions = new ArrayList<>();


        allPhotos = (LinearLayout)findViewById(R.id.allFotos);
        specificPhoto = (LinearLayout)findViewById(R.id.Photo);
        myPos = (LinearLayout)findViewById(R.id.myPos);
        Double currentLat = getIntent().getDoubleExtra("currentLat", 0);
        Double currentLon = getIntent().getDoubleExtra("currentLon", 0);
        int currentPlantId = getIntent().getIntExtra("positionCurr",-1);

        allPhotos.setOnClickListener(new View.OnClickListener() {
            int count = 0;
            @Override
            public void onClick(View v) {
                LatLng plantPos = new LatLng(positions.get(count).latitude,positions.get(count).longitude);
                count++;
                if(count == positions.size() - 1){
                    count = 0;
                }
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(plantPos, 35));
            }
        });

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();

        final ArrayList<Photo> photos = (ArrayList<Photo>) bundle.getSerializable("key");
        Log.d("photos",String.valueOf(photos.size()));

        if (currentLat != null && currentLon != null && currentLat != 0 && currentLon != 0) {
            currentZone = new LatLng(currentLat, currentLon);
            mMap.addMarker(new MarkerOptions().position(currentZone).title("Marker in Portugal"));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentZone, 15));
            found = true;
        }

        BitmapDescriptor bitmapDescriptor;

        LatLng latLng;
        int positionSave = -1;
        int index = 0;


        for (final Photo p : photos) {
            if (p.getLat() != null && p.getLon() != null) // check for 0.0
            {
                if(p.getId() == currentPlantId ){
                    bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW);
                    plantLocation = new LatLng(p.getLat(),p.getLon());
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(plantLocation, 35f));
                    found = true;
                    mMap.addMarker(new MarkerOptions().position(plantLocation)
                            .icon(bitmapDescriptor))
                            .setTag(p);
                }
                else{
                    bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);
                    plantLocation = new LatLng(p.getLat(),p.getLon());
                    positions.add(plantLocation);
                    positionSave = index;
                    mMap.addMarker(new MarkerOptions().position(plantLocation)
                            .icon(bitmapDescriptor))
                            .setTag(p);
                }
                    photosLocation = new LatLng(p.getLat(), p.getLon());


            }
            index++;
        }
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

  @Override
        public boolean onMarkerClick(Marker marker) {
            Photo photo = new Photo();
            Boolean exist = false;
            List <Address> address = new ArrayList<Address>();
               photo = (Photo)marker.getTag();
            Log.d("ph123",String.valueOf(photo.getId()));

            AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);

            final AlertDialog dialog = builder.create();
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            LayoutInflater inflater = MapsActivity.this.getLayoutInflater();
            View view = inflater.inflate(R.layout.custom_dialog,null);
            ImageView image = (ImageView)view.findViewById(R.id.imageView1);
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            TextView countryTxt = (TextView)view.findViewById(R.id.country);
            TextView cityTxt = (TextView)view.findViewById(R.id.city);
            TextView streetTxt = (TextView)view.findViewById(R.id.street);
            String street = null;
            String city = null;
            String country = null;
            try {
                Geocoder geocoder;
                geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());

                address = geocoder.getFromLocation(photo.getLat(),photo.getLon(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
            } catch (IOException e) {
                e.printStackTrace();
            }

            if(address != null && address.size() > 0){
                street = address.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                city = getCity(address);

                if(street == null) {
                    street = address.get(0).getThoroughfare();
                }
                country = address.get(0).getCountryName();
            }


            if(street == null){
                street = "desconhecido";
            }

            if(country == null){
                country = "desconhecido";
            }

            countryTxt.setText(country);
            cityTxt.setText(city);
            streetTxt.setText(street);

            loadPhoto(photo.getPath(),image);
            dialog.setView(view);
            //searching marker id in locationDetailses and getting all the information of a particular marker


            // Create the AlertDialog
            //AlertDialog dialog = builder.create();
            dialog.show();


            // Add the button



        return false;
    }
});
Photo photo = new Photo();
photo = (Photo)marker.getTag();
Log.d("ph123",String.valueOf(photo.getId())); // wrong id here
然后在地图活动中,我对它们进行如下初始化:

 public void showMap(View view){
        Intent i = new Intent(this,MapsActivity.class);
        i.putExtra("currentLat",location.getLatitude());
        i.putExtra("currentLon",location.getLongitude());
        i.putExtra("positionCurr",pos);
        Bundle bundle = new Bundle();
        bundle.putSerializable("key", photos);
        i.putExtras(bundle);
        startActivity(i);
    }
public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        Marker marker;
        LatLng currentZone = null;
        LatLng plantLocation = null;
        LatLng photosLocation = null;
        positions = new ArrayList<>();


        allPhotos = (LinearLayout)findViewById(R.id.allFotos);
        specificPhoto = (LinearLayout)findViewById(R.id.Photo);
        myPos = (LinearLayout)findViewById(R.id.myPos);
        Double currentLat = getIntent().getDoubleExtra("currentLat", 0);
        Double currentLon = getIntent().getDoubleExtra("currentLon", 0);
        int currentPlantId = getIntent().getIntExtra("positionCurr",-1);

        allPhotos.setOnClickListener(new View.OnClickListener() {
            int count = 0;
            @Override
            public void onClick(View v) {
                LatLng plantPos = new LatLng(positions.get(count).latitude,positions.get(count).longitude);
                count++;
                if(count == positions.size() - 1){
                    count = 0;
                }
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(plantPos, 35));
            }
        });

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();

        final ArrayList<Photo> photos = (ArrayList<Photo>) bundle.getSerializable("key");
        Log.d("photos",String.valueOf(photos.size()));

        if (currentLat != null && currentLon != null && currentLat != 0 && currentLon != 0) {
            currentZone = new LatLng(currentLat, currentLon);
            mMap.addMarker(new MarkerOptions().position(currentZone).title("Marker in Portugal"));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentZone, 15));
            found = true;
        }

        BitmapDescriptor bitmapDescriptor;

        LatLng latLng;
        int positionSave = -1;
        int index = 0;


        for (final Photo p : photos) {
            if (p.getLat() != null && p.getLon() != null) // check for 0.0
            {
                if(p.getId() == currentPlantId ){
                    bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW);
                    plantLocation = new LatLng(p.getLat(),p.getLon());
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(plantLocation, 35f));
                    found = true;
                    mMap.addMarker(new MarkerOptions().position(plantLocation)
                            .icon(bitmapDescriptor))
                            .setTag(p);
                }
                else{
                    bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);
                    plantLocation = new LatLng(p.getLat(),p.getLon());
                    positions.add(plantLocation);
                    positionSave = index;
                    mMap.addMarker(new MarkerOptions().position(plantLocation)
                            .icon(bitmapDescriptor))
                            .setTag(p);
                }
                    photosLocation = new LatLng(p.getLat(), p.getLon());


            }
            index++;
        }
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

  @Override
        public boolean onMarkerClick(Marker marker) {
            Photo photo = new Photo();
            Boolean exist = false;
            List <Address> address = new ArrayList<Address>();
               photo = (Photo)marker.getTag();
            Log.d("ph123",String.valueOf(photo.getId()));

            AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);

            final AlertDialog dialog = builder.create();
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            LayoutInflater inflater = MapsActivity.this.getLayoutInflater();
            View view = inflater.inflate(R.layout.custom_dialog,null);
            ImageView image = (ImageView)view.findViewById(R.id.imageView1);
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            TextView countryTxt = (TextView)view.findViewById(R.id.country);
            TextView cityTxt = (TextView)view.findViewById(R.id.city);
            TextView streetTxt = (TextView)view.findViewById(R.id.street);
            String street = null;
            String city = null;
            String country = null;
            try {
                Geocoder geocoder;
                geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());

                address = geocoder.getFromLocation(photo.getLat(),photo.getLon(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
            } catch (IOException e) {
                e.printStackTrace();
            }

            if(address != null && address.size() > 0){
                street = address.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                city = getCity(address);

                if(street == null) {
                    street = address.get(0).getThoroughfare();
                }
                country = address.get(0).getCountryName();
            }


            if(street == null){
                street = "desconhecido";
            }

            if(country == null){
                country = "desconhecido";
            }

            countryTxt.setText(country);
            cityTxt.setText(city);
            streetTxt.setText(street);

            loadPhoto(photo.getPath(),image);
            dialog.setView(view);
            //searching marker id in locationDetailses and getting all the information of a particular marker


            // Create the AlertDialog
            //AlertDialog dialog = builder.create();
            dialog.show();


            // Add the button



        return false;
    }
});
Photo photo = new Photo();
photo = (Photo)marker.getTag();
Log.d("ph123",String.valueOf(photo.getId())); // wrong id here
任何帮助都将不胜感激

谢谢