在 google maps V2 android studio 中单击 InfoWindowClick 时,我如何指定唯一键?
How can I intent a unique key when InfoWindowClick was click in google maps V2 android studio?
我想指定另一个 activity 成功运行所需的两个唯一键,但是当涉及到标记中的 infowindowclick 时,我不知道如何指定这两个唯一键。有没有一种方法可以正确传递数据而不是将这些键添加到代码段中?请看下面我的代码。
// I am getting the values of the store here including the storeID
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(latLng);
for (DocumentSnapshot storeDS: queryDocumentSnapshots){
StoreDetailsModel detailsModel = storeDS.toObject(StoreDetailsModel.class);
GeoPoint storeAddress = detailsModel.getStoreLocation();
double LatitudeData = storeAddress.getLatitude();
double LongitudeData = storeAddress.getLongitude();
final LatLng SlatLng = new LatLng(LatitudeData, LongitudeData);
if (SphericalUtil.computeDistanceBetween(latLng, SlatLng) < 35000){
String storeID = detailsModel.getStoreID();
String storeName = detailsModel.getStoreName();
String storeClass = detailsModel.getStoreClassification();
HashMap storeImg = detailsModel.getStoreImage();
String image = storeImg.get("profile").toString();
addCustomMarkerFromURL(SlatLng, image, storeName, storeClass, mMap, storeID, uid);
builder.include(SlatLng);
}
}
LatLngBounds bounds = builder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 200);
mMap.moveCamera(cameraUpdate);
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
mMap.setOnInfoWindowClickListener(MapFragment.this);
...
//Then here is the method for infoWindowClick Listener
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(this, StoreDetailsActivity.class)
//Here I have a trouble finding a way to pass the data for the storeID and buyerID
//intent.putExtra("storeID", storeID);
//intent.putExtra("buyerID", buyerID);
startActivity(intent);
}
addCustomMarkerFromURL() 方法:
private void addCustomMarkerFromURL(final LatLng slatLng, String image, final String storeName, final String storeClass, final GoogleMap mMap, final String storeID, final String uid) {
if (mMap == null) {
return;
}
Glide.with(getActivity())
.asBitmap()
.load(image).fitCenter()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) {
// mMap.setInfoWindowAdapter(new GoogleInfoWindowAdapter(getActivity(), storeID, uid));
mMap.addMarker(new MarkerOptions()
.position(slatLng)
.icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(getActivity(), resource, storeName))).title(storeClass));
}
});
}
getMarkerBitmapFromView() 方法
private Bitmap getMarkerBitmapFromView(FragmentActivity activity, Bitmap resource, String storeName) {
View view = ((LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.bitmap_google_marker_layout_item, null);
TextView txt_name = (TextView) view.findViewById(R.id.name);
txt_name.setText(storeName);
de.hdodenhof.circleimageview.CircleImageView image = (de.hdodenhof.circleimageview.CircleImageView) view.findViewById(R.id.store_profile_image);
image.setImageBitmap(resource);
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = view.getBackground();
if (drawable != null)
drawable.draw(canvas);
view.draw(canvas);
return returnedBitmap;
}
您可以使用标签代替代码段。
创建具有 2 个属性的 class "storeId" n "buyerId"
public class MyRefId {
private int storeID;
private int buyerID;
public MyRefId(int storeID, int buyerID) {
this.storeID = storeID;
this.buyerID = buyerID;
}
public int getStoreID() {
return storeID;
}
public void setStoreID(int storeID) {
this.storeID = storeID;
}
public int getBuyerID() {
return buyerID;
}
public void setBuyerID(int buyerID) {
this.buyerID = buyerID;
}
}
在 addCustomMarkerFromURL() 中
private void addCustomMarkerFromURL(final LatLng slatLng, String image, final String storeName, final String storeClass, final GoogleMap mMap, final String storeID, final String uid) {
if (mMap == null) {
return;
}
Glide.with(getActivity())
.asBitmap()
.load(image).fitCenter()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) {
// to make it clear for you I do it this way
// start by new MarkerOption()
MarkerOptions markerOptions = new MarkerOptions()
.position(slatLng)
.icon(BitmapDescriptorFactory.fromBitmap(
getMarkerBitmapFromView(getActivity(), resource, storeName))).title(storeClass);
// when you call mMap.addMarker(markerOptions) a new
// marker will be added to the map
// this method also return those marker
// to set tag u can do sth like:
Marker marker = mMap.addMarker(markerOptions);
marker.setTag(new MyRefId(storID,uid));
// or
// mMap.addMarker(markerOptions).setTag(new MyRefId(storID,uid));
}
});
}
那么这里是infoWindowClick Listener的方法
@Override
public void onInfoWindowClick(Marker marker) {
// get marker tag
MyRefId refId = (MyRefId) marker.getTag();
if(refId != null){
Intent intent = new Intent(this, StoreDetailsActivity.class);
intent.putExtra("storeID", refId.getStoreID());
intent.putExtra("buyerID", refId.getBuyerID());
startActivity(intent);
}
}
我想指定另一个 activity 成功运行所需的两个唯一键,但是当涉及到标记中的 infowindowclick 时,我不知道如何指定这两个唯一键。有没有一种方法可以正确传递数据而不是将这些键添加到代码段中?请看下面我的代码。
// I am getting the values of the store here including the storeID
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(latLng);
for (DocumentSnapshot storeDS: queryDocumentSnapshots){
StoreDetailsModel detailsModel = storeDS.toObject(StoreDetailsModel.class);
GeoPoint storeAddress = detailsModel.getStoreLocation();
double LatitudeData = storeAddress.getLatitude();
double LongitudeData = storeAddress.getLongitude();
final LatLng SlatLng = new LatLng(LatitudeData, LongitudeData);
if (SphericalUtil.computeDistanceBetween(latLng, SlatLng) < 35000){
String storeID = detailsModel.getStoreID();
String storeName = detailsModel.getStoreName();
String storeClass = detailsModel.getStoreClassification();
HashMap storeImg = detailsModel.getStoreImage();
String image = storeImg.get("profile").toString();
addCustomMarkerFromURL(SlatLng, image, storeName, storeClass, mMap, storeID, uid);
builder.include(SlatLng);
}
}
LatLngBounds bounds = builder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 200);
mMap.moveCamera(cameraUpdate);
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
mMap.setOnInfoWindowClickListener(MapFragment.this);
...
//Then here is the method for infoWindowClick Listener
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(this, StoreDetailsActivity.class)
//Here I have a trouble finding a way to pass the data for the storeID and buyerID
//intent.putExtra("storeID", storeID);
//intent.putExtra("buyerID", buyerID);
startActivity(intent);
}
addCustomMarkerFromURL() 方法:
private void addCustomMarkerFromURL(final LatLng slatLng, String image, final String storeName, final String storeClass, final GoogleMap mMap, final String storeID, final String uid) {
if (mMap == null) {
return;
}
Glide.with(getActivity())
.asBitmap()
.load(image).fitCenter()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) {
// mMap.setInfoWindowAdapter(new GoogleInfoWindowAdapter(getActivity(), storeID, uid));
mMap.addMarker(new MarkerOptions()
.position(slatLng)
.icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(getActivity(), resource, storeName))).title(storeClass));
}
});
}
getMarkerBitmapFromView() 方法
private Bitmap getMarkerBitmapFromView(FragmentActivity activity, Bitmap resource, String storeName) {
View view = ((LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.bitmap_google_marker_layout_item, null);
TextView txt_name = (TextView) view.findViewById(R.id.name);
txt_name.setText(storeName);
de.hdodenhof.circleimageview.CircleImageView image = (de.hdodenhof.circleimageview.CircleImageView) view.findViewById(R.id.store_profile_image);
image.setImageBitmap(resource);
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = view.getBackground();
if (drawable != null)
drawable.draw(canvas);
view.draw(canvas);
return returnedBitmap;
}
您可以使用标签代替代码段。
创建具有 2 个属性的 class "storeId" n "buyerId"
public class MyRefId {
private int storeID;
private int buyerID;
public MyRefId(int storeID, int buyerID) {
this.storeID = storeID;
this.buyerID = buyerID;
}
public int getStoreID() {
return storeID;
}
public void setStoreID(int storeID) {
this.storeID = storeID;
}
public int getBuyerID() {
return buyerID;
}
public void setBuyerID(int buyerID) {
this.buyerID = buyerID;
}
}
在 addCustomMarkerFromURL() 中
private void addCustomMarkerFromURL(final LatLng slatLng, String image, final String storeName, final String storeClass, final GoogleMap mMap, final String storeID, final String uid) {
if (mMap == null) {
return;
}
Glide.with(getActivity())
.asBitmap()
.load(image).fitCenter()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) {
// to make it clear for you I do it this way
// start by new MarkerOption()
MarkerOptions markerOptions = new MarkerOptions()
.position(slatLng)
.icon(BitmapDescriptorFactory.fromBitmap(
getMarkerBitmapFromView(getActivity(), resource, storeName))).title(storeClass);
// when you call mMap.addMarker(markerOptions) a new
// marker will be added to the map
// this method also return those marker
// to set tag u can do sth like:
Marker marker = mMap.addMarker(markerOptions);
marker.setTag(new MyRefId(storID,uid));
// or
// mMap.addMarker(markerOptions).setTag(new MyRefId(storID,uid));
}
});
}
那么这里是infoWindowClick Listener的方法
@Override
public void onInfoWindowClick(Marker marker) {
// get marker tag
MyRefId refId = (MyRefId) marker.getTag();
if(refId != null){
Intent intent = new Intent(this, StoreDetailsActivity.class);
intent.putExtra("storeID", refId.getStoreID());
intent.putExtra("buyerID", refId.getBuyerID());
startActivity(intent);
}
}