Commit 4d19ee36 authored by Qiu Xiang's avatar Qiu Xiang

重写 InfoWindow 的默认 UI,提供更好的显示效果

parent 3a8fcba0
...@@ -34,7 +34,7 @@ public class AMapMarker extends ReactViewGroup { ...@@ -34,7 +34,7 @@ public class AMapMarker extends ReactViewGroup {
private static final Map<String, Float> COLORS; private static final Map<String, Float> COLORS;
static { static {
COLORS = new HashMap<String, Float>(); COLORS = new HashMap<>();
COLORS.put("HUE_AZURE", BitmapDescriptorFactory.HUE_AZURE); COLORS.put("HUE_AZURE", BitmapDescriptorFactory.HUE_AZURE);
COLORS.put("HUE_BLUE", BitmapDescriptorFactory.HUE_BLUE); COLORS.put("HUE_BLUE", BitmapDescriptorFactory.HUE_BLUE);
COLORS.put("HUE_CYAN", BitmapDescriptorFactory.HUE_CYAN); COLORS.put("HUE_CYAN", BitmapDescriptorFactory.HUE_CYAN);
...@@ -56,7 +56,7 @@ public class AMapMarker extends ReactViewGroup { ...@@ -56,7 +56,7 @@ public class AMapMarker extends ReactViewGroup {
private float opacity = 1; private float opacity = 1;
private boolean draggable = false; private boolean draggable = false;
private boolean selected; private boolean selected;
private boolean infoWindowEnabled = false; private boolean infoWindowEnabled = true;
private BitmapDescriptor bitmapDescriptor; private BitmapDescriptor bitmapDescriptor;
private RCTEventEmitter eventEmitter; private RCTEventEmitter eventEmitter;
private DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber = private DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber =
......
package cn.qiuxiang.react.amap3d; package cn.qiuxiang.react.amap3d;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.location.Location; import android.location.Location;
import android.view.View; import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.amap.api.maps.AMap; import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView; import com.amap.api.maps.MapView;
...@@ -119,7 +123,25 @@ public class AMapView extends MapView { ...@@ -119,7 +123,25 @@ public class AMapView extends MapView {
@Override @Override
public View getInfoContents(Marker marker) { public View getInfoContents(Marker marker) {
return null; LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
TextView titleView = new TextView(context);
titleView.setText(marker.getTitle());
titleView.setTextColor(Color.parseColor("#212121"));
layout.addView(titleView);
String snippet = marker.getSnippet();
if (!snippet.isEmpty()) {
TextView snippetView = new TextView(context);
snippetView.setText(snippet);
snippetView.setSingleLine(false);
snippetView.setMaxEms(12);
snippetView.setPadding(0, (int) pxFromDp(context, 5), 0, 0);
snippetView.setTextColor(Color.parseColor("#757575"));
layout.addView(snippetView);
}
return layout;
} }
}); });
} }
...@@ -132,4 +154,8 @@ public class AMapView extends MapView { ...@@ -132,4 +154,8 @@ public class AMapView extends MapView {
public void sendEvent(String name, WritableMap data) { public void sendEvent(String name, WritableMap data) {
eventEmitter.receiveEvent(getId(), name, data); eventEmitter.receiveEvent(getId(), name, data);
} }
private static float pxFromDp(Context context, float dp) {
return dp * context.getResources().getDisplayMetrics().density;
}
} }
...@@ -28,7 +28,6 @@ export default class MarkerComponent extends Component { ...@@ -28,7 +28,6 @@ export default class MarkerComponent extends Component {
return <MapView style={StyleSheet.absoluteFill}> return <MapView style={StyleSheet.absoluteFill}>
<Marker <Marker
draggable draggable
showsInfoWindow
title='一个可拖拽的 Marker' title='一个可拖拽的 Marker'
onDragEnd={({nativeEvent}) => onDragEnd={({nativeEvent}) =>
Alert.alert(`新坐标:${nativeEvent.latitude}, ${nativeEvent.longitude}`)} Alert.alert(`新坐标:${nativeEvent.latitude}, ${nativeEvent.longitude}`)}
...@@ -40,7 +39,6 @@ export default class MarkerComponent extends Component { ...@@ -40,7 +39,6 @@ export default class MarkerComponent extends Component {
/> />
<Marker <Marker
selected selected
showsInfoWindow
icon='HUE_RED' icon='HUE_RED'
title='一个红色的 Marker' title='一个红色的 Marker'
infoWindowWidth={100} infoWindowWidth={100}
...@@ -49,21 +47,20 @@ export default class MarkerComponent extends Component { ...@@ -49,21 +47,20 @@ export default class MarkerComponent extends Component {
longitude: 116.297972, longitude: 116.297972,
}}> }}>
<InfoWindow style={styles.customInfoWindow}> <InfoWindow style={styles.customInfoWindow}>
<Text>一个自定义 View 的信息窗口</Text> <Text>Custom View InfoWindow</Text>
</InfoWindow> </InfoWindow>
</Marker> </Marker>
<Marker <Marker
showsInfoWindow
icon={require('../images/marker.png')} icon={require('../images/marker.png')}
title='自定义图片' title='自定义图片'
description="Note the use of nativeOnly above. Sometimes you'll have some special properties that you need to expose for the native component, but don't actually want them as part of the API for the associated React component."
coordinate={{ coordinate={{
latitude: 39.906901, latitude: 39.906901,
longitude: 116.397972, longitude: 116.397972,
}} }}
/> />
<Marker <Marker
showsInfoWindow title='Custom View Marker'
title='自定义 View Marker'
icon={() => <Overlay style={styles.customMarker}> icon={() => <Overlay style={styles.customMarker}>
<Text style={styles.markerText}>{this.state.time.toLocaleTimeString()}</Text> <Text style={styles.markerText}>{this.state.time.toLocaleTimeString()}</Text>
</Overlay>} </Overlay>}
...@@ -79,12 +76,12 @@ export default class MarkerComponent extends Component { ...@@ -79,12 +76,12 @@ export default class MarkerComponent extends Component {
const styles = StyleSheet.create({ const styles = StyleSheet.create({
customInfoWindow: { customInfoWindow: {
backgroundColor: '#fff', backgroundColor: '#fff',
width: 128, position: 'absolute',
padding: 10, padding: 10,
elevation: 4, elevation: 4,
}, },
customMarker: { customMarker: {
width: 70, position: 'absolute',
backgroundColor: '#009688', backgroundColor: '#009688',
alignItems: 'center', alignItems: 'center',
padding: 5, padding: 5,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment