我创建了一个自定义视图,以在其他片段或活动中显示地图。但是我想像Google Maps应用程序一样,简单地单击框架。有人可以帮我吗?
有自定义视图:
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的活动/片段中实现GoogleMap.OnMapClickListener
。
实施后,将覆盖方法放入您的片段/活动中:
@Override
public void onMapClick(LatLng latLng) {
if (Logger.DEBUG) Log.d(TAG, "onMapClick");
// Do something
}