我正在尝试在LongPressEvent上添加地图覆盖地图,在此之后将地图居中,但即使没有将地图居中,它也总是在显示叠加之前显示一个蓝色图钉(heremaps默认图钉(。
有没有办法防止在添加地图叠加层时显示蓝色图钉?
使用更多代码更新:
坐标
private class MapGestureListener implements MapGesture.OnGestureListener{
...
@Override
public boolean onLongPressEvent(PointF pointF) {
GeoCoordinate gc =map.pixelToGeo(pointF);
if(sdkMapSettingsListener!=null)
sdkMapSettingsListener.onLongPress(gc);
return false; //i've tried returning true and then use map.setCenter and the problem persists.
}
...
then on my listener:
@Override
public void onLongPress(GeoCoordinate gc) {
if(mo!=null)
{
map.removeMapOverlay(mo);
mo=null;
}//remove old overlay if exists
View view = inflater.inflate(R.layout.custom_map_info_window, null);
RelativeLayout info_window_wrapper = view.findViewById(R.id.info_window_wrapper);
info_window_wrapper.setBackgroundTintList(ColorStateList.valueOf(ThemeUtils.getEntityColor(GPSApplication.getInstance(),Constants.EntityTheme.colorPrimary)));
ImageView imgPlaceIcon = view.findViewById(R.id.imageView_placeIcon);
imgPlaceIcon.setColorFilter(ThemeUtils.getEntityColor(GPSApplication.getInstance(),Constants.EntityTheme.colorAccent));
ImageView ivPlaceMarker = view.findViewById(R.id.ivPlaceMarker);
ivPlaceMarker.setColorFilter(ThemeUtils.getEntityColor(GPSApplication.getInstance(),Constants.EntityTheme.colorAccent));
TextView textViewPlaceName = view.findViewById(R.id.textView_PlaceName);
final TextView textViewOpenNow = view.findViewById(R.id.textView_OpenNow);
TextView textViewDistance = view.findViewById(R.id.textView_Distance);
textViewPlaceName.setTextColor(ThemeUtils.getEntityColor(GPSApplication.getInstance(),Constants.EntityTheme.primaryTextColor));
textViewDistance.setTextColor(ThemeUtils.getEntityColor(GPSApplication.getInstance(),Constants.EntityTheme.primaryTextColor));
String sDistance = SearchManager.getInstance().requestDistanceTo(gc,mContext);
textViewPlaceName.setText(sPlace);
textViewDistance.setText(sDistance);
mo = new MapOverlay(view,gc);
mo.setAnchorPoint(new PointF(ScreenUtils.convertDPToPixels(mContext,75),ScreenUtils.convertDPToPixels(mContext,108)));
map.addMapOverlay(mo);
//map.setCenter(gc, Map.Animation.LINEAR);
}
地图叠加视图
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="@+id/info_window_wrapper"
android:layout_width="wrap_content"
android:layout_height="69dp"
android:orientation="vertical"
android:paddingTop="@dimen/margin_xsmall"
android:paddingEnd="@dimen/margin_xsmall"
android:paddingStart="@dimen/margin_xsmall"
android:paddingBottom="25dp"
android:background="@drawable/custom_info_window_shape"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView_PlaceName"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="12sp"
android:text=""/>
<RelativeLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/textView_PlaceName">
<TextView
android:id="@+id/textView_OpenNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="false"
android:textSize="12sp"
android:text="Open" />
<ImageView
android:id="@+id/imageView_placeIcon"
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/textView_Distance"
android:padding="@dimen/margin_xxsmall"
android:src="@drawable/ic_place_white_24dp" />
<TextView
android:id="@+id/textView_Distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:singleLine="false"
android:text=""
android:textSize="12sp" />
</RelativeLayout>
</RelativeLayout>
<ImageView
android:id="@+id/ivPlaceMarker"
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY"
android:src="@drawable/ic_place_white_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/info_window_wrapper" />
</androidx.constraintlayout.widget.ConstraintLayout>
第一个屏幕是首先显示的内容(蓝色图钉(,然后是第二个屏幕(地图叠加(
更新视频:https://www.youtube.com/watch?v=qNuZNUWDxeQ
更新 2 示例活动类似于我们的项目,发生此问题且未添加标记:
public class TheThirdActivity extends AppCompatActivity {
private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;
/**
* Permissions that need to be explicitly requested from end user.
*/
private static final String[] REQUIRED_SDK_PERMISSIONS = new String[] {
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE };
private Map map = null;
private SupportMapFragment mapFragment = null;
MapOverlay mo;
Context mContext;
LayoutInflater inflater;
SDKMapSettingsListener sdkMapSettingsListener;
public interface SDKMapSettingsListener {
void onLongPress(GeoCoordinate gc);
}
public void setSdkMapSettingsListener(SDKMapSettingsListener listener) {
this.sdkMapSettingsListener = listener;
}
private SupportMapFragment getMapFragment() {
return (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
inflater = getLayoutInflater();
setContentView(R.layout.activity_the_third);
mapFragment = getMapFragment();
boolean success = com.here.android.mpa.common.MapSettings.setIsolatedDiskCacheRootPath(
getApplicationContext().getExternalFilesDir(null) + File.separator + ".here-maps",
"com.here.android.tutorial.MapService");
if (!success) {
Toast.makeText(getApplicationContext(), "Unable to set isolated disk cache path.", Toast.LENGTH_LONG);
} else {
mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
map = mapFragment.getMap();
map.setCenter(new GeoCoordinate(41.160465,-8.647859 ), Map.Animation.NONE);
map.setZoomLevel(18);
map.setMapScheme(Map.Scheme.NORMAL_TRAFFIC_NIGHT);
mapFragment.getMapGesture().addOnGestureListener(new MapGesture.OnGestureListener() {
@Override
public boolean onLongPressEvent(PointF pointF) {
GeoCoordinate gc =map.pixelToGeo(pointF);
if(sdkMapSettingsListener!=null)
sdkMapSettingsListener.onLongPress(gc);
return true;
}
@Override
public boolean onTapEvent(PointF pointF) {
if(mo!=null)
map.removeMapOverlay(mo);
mo=null;
return false;
}
@Override
public void onPanStart() {
}
@Override
public void onPanEnd() {
}
@Override
public void onMultiFingerManipulationStart() {
}
@Override
public void onMultiFingerManipulationEnd() {
}
@Override
public boolean onMapObjectsSelected(List<ViewObject> list) {
return false;
}
@Override
public boolean onDoubleTapEvent(PointF pointF) {
return false;
}
@Override
public void onPinchLocked() {
}
@Override
public boolean onPinchZoomEvent(float v, PointF pointF) {
return false;
}
@Override
public void onRotateLocked() {
}
@Override
public boolean onRotateEvent(float v) {
return false;
}
@Override
public boolean onTiltEvent(float v) {
return false;
}
@Override
public void onLongPressRelease() {
}
@Override
public boolean onTwoFingerTapEvent(PointF pointF) {
return false;
}
}, 1, true);
sdkMapSettingsListener = new SDKMapSettingsListener() {
@Override
public void onLongPress(GeoCoordinate gc) {
if(mo!=null)
{
map.removeMapOverlay(mo);
mo=null;
}
View view = inflater.inflate(R.layout.custom_map_info_window, null);
RelativeLayout info_window_wrapper = view.findViewById(R.id.info_window_wrapper);
info_window_wrapper.setBackgroundTintList(ColorStateList.valueOf(Color.BLACK));
ImageView imgPlaceIcon = view.findViewById(R.id.imageView_placeIcon);
imgPlaceIcon.setColorFilter(Color.YELLOW);
ImageView ivPlaceMarker = view.findViewById(R.id.ivPlaceMarker);
ivPlaceMarker.setColorFilter(Color.YELLOW);
TextView textViewPlaceName = view.findViewById(R.id.textView_PlaceName);
final TextView textViewOpenNow = view.findViewById(R.id.textView_OpenNow);
TextView textViewDistance = view.findViewById(R.id.textView_Distance);
textViewPlaceName.setTextColor(Color.WHITE);
textViewDistance.setTextColor(Color.WHITE);
String sDistance = requestDistanceTo(gc,mContext);
textViewPlaceName.setText("sample place");
textViewDistance.setText(sDistance);
mo = new MapOverlay(view,gc);
mo.setAnchorPoint(new PointF(convertDPToPixels(mContext,75),convertDPToPixels(mContext,108)));
map.addMapOverlay(mo);
map.setCenter(gc, Map.Animation.LINEAR);
}
};
} else {
System.out.println("ERROR: Cannot initialize Map Fragment");
}
}
});
}
}
public int convertDPToPixels(Context context, int dip) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, displayMetrics);
}
public String requestDistanceTo(@NonNull GeoCoordinate geoCoordinate, @NonNull Context mContext)
{
String sFinal="";
double dDist=0;
if(PositioningManager.getInstance().getPosition().getCoordinate().isValid())
{
dDist= PositioningManager.getInstance().getPosition().getCoordinate().distanceTo(geoCoordinate);
}
else
{
if(PositioningManager.getInstance().getLastKnownPosition().getCoordinate().isValid())
dDist= PositioningManager.getInstance().getLastKnownPosition().getCoordinate().distanceTo(geoCoordinate);
else
return "";
}
long lDist = (long) dDist;
sFinal= formatInMetricSystem(mContext,lDist);
return sFinal;
}
private static final NumberFormat FORMATTER = NumberFormat.getInstance();
public static String formatInMetricSystem(final Context context, final long distance) {
final String value;
final String unit;
if (distance < 0) { // invalid
value = "--";
unit = "m";
} else if (distance < 1000) {
value = FORMATTER.format(distance);
unit = "m";
} else {
value = FORMATTER.format(Math.round((double) distance / 1000));
unit = "km";
}
return String.format("%1$s%2$s", value, unit);
}
}
查看您的第一个屏幕截图,MapMarker(图钉(可以在地图上显示的唯一方法是将其添加到代码中的某个位置。 从第二个屏幕截图来看,看起来MapOverlay只是覆盖了MapMarker(pin(。您能否检查您的代码以查看您是否正在将 MapMarker 添加到地图并将其删除(map.removeMapObject(<map marker>)
.