我创建了一个自定义视图,以在其他片段或活动中显示地图。 但是我希望得到一个简单的框架点击,就像谷歌地图应用程序一样。 有人可以帮帮我吗?
有自定义视图:
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();
}
});
答案 0 :(得分:1)
搜索一下,我找到了一种验证地图中简单点击的方法。
在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的活动/片段中实现GoogleMap.OnMapClickListener
。
实现后,在您的片段/活动中放置override方法:
@Override
public void onMapClick(LatLng latLng) {
if (Logger.DEBUG) Log.d(TAG, "onMapClick");
// Do something
}