Weex开发之地图篇的具体使用(5)

@Override protected FrameLayout initComponentHostView(@NonNull Context context) { mapContainer = new FrameLayout(context) { @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // 解决与Scroller的滑动冲突 if (ev.getAction() == MotionEvent.ACTION_UP) { requestDisallowInterceptTouchEvent(false); } else { requestDisallowInterceptTouchEvent(true); } return false; } }; mapContainer.setBackgroundColor(fakeBackgroundColor); if (context instanceof Activity) { mActivity = (Activity) context; } return mapContainer; } @Override protected void setHostLayoutParams(FrameLayout host, int width, int height, int left, int right, int top, int bottom) { super.setHostLayoutParams(host, width, height, left, right, top, bottom); if (!isMapLoaded.get() && !isInited.get()) { isInited.set(true); mapContainer.postDelayed(new Runnable() { @Override public void run() { mMapView = new TextureMapView(getContext()); mapContainer.addView(mMapView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); WXLogUtils.e(TAG, "Create MapView " + mMapView.toString()); initMap(); } }, 0); } } private void initMap() { mMapView.onCreate(null); isMapLoaded.set(false); if (mAMap == null) { mAMap = mMapView.getMap(); mAMap.setInfoWindowAdapter(new InfoWindowAdapter(this)); mAMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() { @Override public void onMapLoaded() { WXLogUtils.e(TAG, "Map loaded"); isMapLoaded.set(true); mZoomLevel = mAMap.getCameraPosition().zoom; mMapView.postDelayed(new Runnable() { @Override public void run() { execPaddingTasks(); } }, 16); } }); // 绑定 Marker 被点击事件 mAMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() { // marker 对象被点击时回调的接口 // 返回 true 则表示接口已响应事件,否则返回false @Override public boolean onMarkerClick(Marker marker) { if (marker != null) { for (int i = 0; i < getChildCount(); i++) { if (getChild(i) instanceof DMapMarkerComponent) { DMapMarkerComponent child = (DMapMarkerComponent) getChild(i); if (child.getMarker() != null && child.getMarker().getId() == marker.getId()) { child.onClick(); } } } } return false; } }); mAMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() { private boolean mZoomChanged; @Override public void onCameraChange(CameraPosition cameraPosition) { mZoomChanged = mZoomLevel != cameraPosition.zoom; mZoomLevel = cameraPosition.zoom; } @Override public void onCameraChangeFinish(CameraPosition cameraPosition) { if (mZoomChanged) { float scale = mAMap.getScalePerPixel(); float scaleInWeex = scale / WXViewUtils.getWeexPxByReal(scale); VisibleRegion visibleRegion = mAMap.getProjection().getVisibleRegion(); WXLogUtils.d(TAG, "Visible region: " + visibleRegion.toString()); Map<String, Object> region = new HashMap<>(); region.put("northeast", convertLatLng(visibleRegion.latLngBounds.northeast)); region.put("southwest", convertLatLng(visibleRegion.latLngBounds.southwest)); Map<String, Object> data = new HashMap<>(); data.put("targetCoordinate", cameraPosition.target.toString()); data.put("zoom", cameraPosition.zoom); data.put("tilt", cameraPosition.tilt); data.put("bearing", cameraPosition.bearing); data.put("isAbroad", cameraPosition.isAbroad); data.put("scalePerPixel", scaleInWeex); data.put("visibleRegion", region); getInstance().fireEvent(getRef(), WeexConstant.EVENT.ZOOM_CHANGE, data); } } }); mAMap.setOnMapTouchListener(new AMap.OnMapTouchListener() { boolean dragged = false; @Override public void onTouch(MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_MOVE: dragged = true; break; case MotionEvent.ACTION_UP: if (dragged) getInstance().fireEvent(getRef(), WeexConstant.EVENT.DRAG_CHANGE); dragged = false; break; } } }); setUpMap(); } } }

3.2 weex-amap-info-window

当然,我们也可以使用它实现weex-amap-info-window功能,虽然weex-amap-info-window已经被内置到weex-amap中。是的的思路如下:

新建DMapViewMarkerComponent类继承WXComponent;

在DMapViewComponent中使用mapview的addMarker方法添加DMapViewMarkerComponent组件 。

在DMapViewComponent中使用mapview的addMarker方法添加DMapViewMarkerComponent组件 。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6aad1e73875f62d2873f0909d7946d2b.html