在 Map CustomView 中添加 onClickListener
Add onClickListener in a Map CustomView
我创建了一个自定义视图以在其他片段或活动中显示地图。
但我想在框架中进行简单的点击,就像 google 地图应用程序所做的那样。
有人可以帮助我吗?
有自定义视图:
public class MapView extends FrameLayout {
private Subject<GoogleMap> mapSubject;
private static final float MAP_ZOOM_LEVEL_STATIC = 12.0f;
private Circle mCircle;
public MapView(@NonNull Context context) {
super(context);
init(context, null);
}
public MapView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MapView(@NonNull Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
if (!isInEditMode()) {
FragmentTransaction fragmentTransaction =((AppCompatActivity)context)
.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(getId(), mapFragment);
fragmentTransaction.commit();
mapSubject = BehaviorSubject.create();
Observable.create((ObservableOnSubscribe<GoogleMap>) e -> mapFragment.getMapAsync(e::onNext))
.subscribe(mapSubject);
mapSubject.subscribe(googleMap -> mCircle = googleMap.addCircle(new
CircleOptions().center(new LatLng(0.0f, 0.0f)).radius(0)));
}
}
/**
* Update position and create a new Circle and move camera to new position
*
* @param location The location to be updated.
*/
public void updatePosition(LatLng location) {
mapSubject.subscribe(googleMap -> {
mCircle.remove();
mCircle = googleMap.addCircle(new
CircleOptions().center(location).radius(15)
.strokeWidth(10)
.strokeColor(Color.RED)
.fillColor(Color.RED));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,
MAP_ZOOM_LEVEL_STATIC));
});
}
/**
* This method is responsible to add a polyline in google maps
*
* @param results array with directions
*/
public void addPolyline(DirectionsResult results) {
if(results != null) {
mapSubject.subscribe(googleMap -> {
List<LatLng> decodedPath =
PolyUtil.decode(results.routes[0]
.overviewPolyline
.getEncodedPath());
googleMap.addPolyline(new
PolylineOptions()
.addAll(decodedPath)
.width(10)
.color(Color.RED)
.geodesic(true));
updatePosition(decodedPath.get(0));
});
}
}
}
这就是我在 xml 中使用的方式:
<com.name.of.package.custom_view.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
这就是我尝试识别点击的方式(没有成功,因为在调试器中没有通过这里):
MapView mapView = (MapView)findViewById(R.id.mapView)
mapView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toogle(view);
Toast.makeText(mContext, "Map clicked!", Toast.LENGTH_SHORT).show();
}
});
搜索了一下,我找到了一种验证简单点击地图的方法。
在 MapView.java
中创建函数
/**
* This method is responsible to add a MapClickListener to handle simple clicks in map.
*
* @param mapClickListener class that will handle simple click
*/
public void setOnMapClick(GoogleMap.OnMapClickListener mapClickListener) {
mapSubject.subscribe(googleMap -> googleMap.setOnMapClickListener(mapClickListener));
}
之后在使用mapView的时候设置监听器,例如:
mapView.setOnMapClick(this);
PS.: 要放置 this
,您需要在您正在使用 mapView 的 activity/fragment 中实施 GoogleMap.OnMapClickListener
。
实施后,将覆盖方法放入您的 fragment/activity:
@Override
public void onMapClick(LatLng latLng) {
if (Logger.DEBUG) Log.d(TAG, "onMapClick");
// Do something
}
我创建了一个自定义视图以在其他片段或活动中显示地图。 但我想在框架中进行简单的点击,就像 google 地图应用程序所做的那样。 有人可以帮助我吗?
有自定义视图:
public class MapView extends FrameLayout {
private Subject<GoogleMap> mapSubject;
private static final float MAP_ZOOM_LEVEL_STATIC = 12.0f;
private Circle mCircle;
public MapView(@NonNull Context context) {
super(context);
init(context, null);
}
public MapView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MapView(@NonNull Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
if (!isInEditMode()) {
FragmentTransaction fragmentTransaction =((AppCompatActivity)context)
.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(getId(), mapFragment);
fragmentTransaction.commit();
mapSubject = BehaviorSubject.create();
Observable.create((ObservableOnSubscribe<GoogleMap>) e -> mapFragment.getMapAsync(e::onNext))
.subscribe(mapSubject);
mapSubject.subscribe(googleMap -> mCircle = googleMap.addCircle(new
CircleOptions().center(new LatLng(0.0f, 0.0f)).radius(0)));
}
}
/**
* Update position and create a new Circle and move camera to new position
*
* @param location The location to be updated.
*/
public void updatePosition(LatLng location) {
mapSubject.subscribe(googleMap -> {
mCircle.remove();
mCircle = googleMap.addCircle(new
CircleOptions().center(location).radius(15)
.strokeWidth(10)
.strokeColor(Color.RED)
.fillColor(Color.RED));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,
MAP_ZOOM_LEVEL_STATIC));
});
}
/**
* This method is responsible to add a polyline in google maps
*
* @param results array with directions
*/
public void addPolyline(DirectionsResult results) {
if(results != null) {
mapSubject.subscribe(googleMap -> {
List<LatLng> decodedPath =
PolyUtil.decode(results.routes[0]
.overviewPolyline
.getEncodedPath());
googleMap.addPolyline(new
PolylineOptions()
.addAll(decodedPath)
.width(10)
.color(Color.RED)
.geodesic(true));
updatePosition(decodedPath.get(0));
});
}
}
}
这就是我在 xml 中使用的方式:
<com.name.of.package.custom_view.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
这就是我尝试识别点击的方式(没有成功,因为在调试器中没有通过这里):
MapView mapView = (MapView)findViewById(R.id.mapView)
mapView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toogle(view);
Toast.makeText(mContext, "Map clicked!", Toast.LENGTH_SHORT).show();
}
});
搜索了一下,我找到了一种验证简单点击地图的方法。
在 MapView.java
中创建函数/**
* This method is responsible to add a MapClickListener to handle simple clicks in map.
*
* @param mapClickListener class that will handle simple click
*/
public void setOnMapClick(GoogleMap.OnMapClickListener mapClickListener) {
mapSubject.subscribe(googleMap -> googleMap.setOnMapClickListener(mapClickListener));
}
之后在使用mapView的时候设置监听器,例如:
mapView.setOnMapClick(this);
PS.: 要放置 this
,您需要在您正在使用 mapView 的 activity/fragment 中实施 GoogleMap.OnMapClickListener
。
实施后,将覆盖方法放入您的 fragment/activity:
@Override
public void onMapClick(LatLng latLng) {
if (Logger.DEBUG) Log.d(TAG, "onMapClick");
// Do something
}