我正在使用谷歌地图和标记显示有关图片拍摄地点的一些信息以及其他相关信息。
所以基本上我有一个活动,我有一个与特定植物相关的照片列表,在该活动中我可以选择在地图上看到它。当我按下该选项时,所有照片(有一个位置)都在地图上显示为绿色标记,单击的当前照片将显示为黄色标记。
因此,当我点击“在地图上看到”时,我会传递所有照片的数组,所选照片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++;
}
当我点击标记时显示问题,有时我没有得到点击照片的正确ID,我可以用黄色标记看到它,黄色标记代表我在之前的活动,当我点击地图上的黄色标记时,照片会有所不同。
我已经检查了传递的id并且它是正确的,我试图调试,当我设置标签时一切似乎都很好,但是当我收到id是错的时候,我不知道究竟发生了什么,是因为我无法将实例传递给标记标记?还是我指向同一个对象?需要一些帮助。
以下是标记的onClick:
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
任何帮助都会受到赞赏,
谢谢!