diff --git a/android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapModule.java b/android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapModule.java index 05e370d0..f8b9665d 100644 --- a/android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapModule.java +++ b/android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapModule.java @@ -10,6 +10,7 @@ import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; +import com.baidu.mapapi.map.Marker; import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -50,6 +51,8 @@ public class BaiduMapModule extends ReactContextBaseJavaModule { private LocationClient locationClient; + private Marker marker; + public BaiduMapModule(ReactApplicationContext reactContext) { super(reactContext); context = reactContext; @@ -61,12 +64,15 @@ public String getName() { @ReactMethod public void setMarker(double latitude, double longitude) { + if(marker != null) { + marker.remove(); + } LatLng point = new LatLng(latitude, longitude); BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.icon_gcoding); OverlayOptions option = new MarkerOptions() .icon(bitmap) .position(point); - getMap().addOverlay(option); + marker = (Marker)getMap().addOverlay(option); } @ReactMethod diff --git a/android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapViewManager.java b/android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapViewManager.java index 7926c64a..402e99af 100644 --- a/android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapViewManager.java +++ b/android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapViewManager.java @@ -1,8 +1,12 @@ package org.lovebing.reactnative.baidumap; import android.content.Context; +import android.graphics.Point; +import android.os.Handler; import android.support.annotation.Nullable; import android.util.Log; +import android.view.View; +import android.view.ViewGroup; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.MapPoi; @@ -11,26 +15,31 @@ import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.SDKInitializer; +import com.baidu.mapapi.map.MapViewLayoutParams; import com.baidu.mapapi.map.Marker; import com.baidu.mapapi.model.LatLng; import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableMap; -import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.ThemedReactContext; +import com.facebook.react.uimanager.ViewGroupManager; import com.facebook.react.uimanager.annotations.ReactProp; import com.facebook.react.uimanager.events.RCTEventEmitter; /** * Created by lovebing on 12/20/2015. */ -public class BaiduMapViewManager extends SimpleViewManager { +public class BaiduMapViewManager extends ViewGroupManager { private static final String REACT_CLASS = "RCTBaiduMapView"; private static MapView mMapView; private ThemedReactContext mReactContext; + private ReadableArray childrenPoints; + public String getName() { return REACT_CLASS; } @@ -52,6 +61,24 @@ public MapView createViewInstance(ThemedReactContext context) { return mMapView; } + @Override + public void addView(MapView parent, View child, int index) { + if(childrenPoints != null) { + Point point = new Point(); + ReadableArray item = childrenPoints.getArray(index); + if(item != null) { + point.set(item.getInt(0), item.getInt(1)); + MapViewLayoutParams mapViewLayoutParams = new MapViewLayoutParams + .Builder() + .layoutMode(MapViewLayoutParams.ELayoutMode.absoluteMode) + .point(point) + .build(); + parent.addView(child, mapViewLayoutParams); + } + } + + } + @ReactProp(name = "zoomControlsVisible") public void setZoomControlsVisible(MapView mapView, boolean zoomControlsVisible) { mapView.showZoomControls(zoomControlsVisible); @@ -69,6 +96,11 @@ public void setZoom(MapView mapView, float zoom) { mapView.getMap().setMapStatus(mapStatusUpdate); } + @ReactProp(name = "childrenPoints") + public void setChildrenPoints(MapView mapView, ReadableArray childrenPoints) { + this.childrenPoints = childrenPoints; + } + /** * * @param mapView diff --git a/demo/BaiduMapDemo.js b/demo/BaiduMapDemo.js index 21ac0385..2ea2d44c 100644 --- a/demo/BaiduMapDemo.js +++ b/demo/BaiduMapDemo.js @@ -61,12 +61,16 @@ export default class BaiduMapDemo extends Component { return ( { - }} - /> + > + ViewGroup is not allowed (Android) + This is not a good idea to add children view to Baidu MapView + + { MapModule.setMapType(MapTypes.NORMAL); @@ -108,7 +112,8 @@ export default class BaiduMapDemo extends Component { const styles = StyleSheet.create({ row: { - flexDirection: 'row' + flexDirection: 'row', + justifyContent: 'flex-start' }, btn: { height: 48, diff --git a/demo/android/app/app.iml b/demo/android/app/app.iml index 5478110d..5891c315 100644 --- a/demo/android/app/app.iml +++ b/demo/android/app/app.iml @@ -64,14 +64,6 @@ - - - - - - - - @@ -80,6 +72,14 @@ + + + + + + + + diff --git a/demo/ios/rn.xcodeproj/project.xcworkspace/xcuserdata/lovebing.xcuserdatad/UserInterfaceState.xcuserstate b/demo/ios/rn.xcodeproj/project.xcworkspace/xcuserdata/lovebing.xcuserdatad/UserInterfaceState.xcuserstate index 9e1d722f..902faeab 100644 Binary files a/demo/ios/rn.xcodeproj/project.xcworkspace/xcuserdata/lovebing.xcuserdatad/UserInterfaceState.xcuserstate and b/demo/ios/rn.xcodeproj/project.xcworkspace/xcuserdata/lovebing.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/index.js b/index.js index 6ce94a5f..270a4be1 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,7 @@ export class MapView extends Component { zoomControlsVisible: PropTypes.bool, mapType: PropTypes.number, zoom: PropTypes.number, + childrenPoints: PropTypes.array, onMapStatusChangeStart: PropTypes.func, onMapStatusChange: PropTypes.func, onMapStatusChangeFinish: PropTypes.func, @@ -34,7 +35,8 @@ export class MapView extends Component { static defaultProps = { zoomControlsVisible: true, - mapType: MapTypes.NORMAL + mapType: MapTypes.NORMAL, + childrenPoints: [] }; constructor() {