Commit 3cc70dfd authored by Qiu Xiang's avatar Qiu Xiang

简化 Region 结构

parent 764885ba
package cn.qiuxiang.react.amap3d
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.facebook.react.bridge.ReadableMap
import android.graphics.Color
/**
* Created by yuekong on 2017/7/4.
*/
class AMapConverter {
companion object {
fun LatLng(map: ReadableMap): LatLng {
return LatLng(map.getDouble("latitude"), map.getDouble("longitude"))
}
fun LatLngDelta(map: ReadableMap): LatLng {
return LatLng(map.getDouble("latitudeDelta"), map.getDouble("longitudeDelta"))
}
fun LatLngBounds(map: ReadableMap): LatLngBounds {
val center = LatLng(map.getMap("center"))
val span = LatLngDelta(map.getMap("span"))
return LatLngBounds(
LatLng(center.latitude - span.latitude, center.longitude - span.longitude),
LatLng(center.latitude + span.latitude, center.longitude + span.longitude)
)
}
fun color(color: Int, opacity: Float): Int {
return Color.argb(((opacity * Color.alpha(color)).toInt()), Color.red(color), Color.green(color), Color.blue(color))
}
}
}
\ No newline at end of file
......@@ -6,12 +6,10 @@ import android.view.View
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.MapView
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MyLocationStyle
import com.amap.api.maps.model.*
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.uimanager.ThemedReactContext
import com.facebook.react.uimanager.events.RCTEventEmitter
......@@ -205,4 +203,15 @@ class AMapView(context: Context) : MapView(context) {
CameraPosition(coordinate, zoomLevel, tilt, rotation))
map.animateCamera(cameraUpdate, duration.toLong(), animateCallback)
}
fun setLimitRegion(limitRegion: ReadableMap) {
val latitude = limitRegion.getDouble("latitude")
val longitude = limitRegion.getDouble("longitude")
val latitudeDelta = limitRegion.getDouble("latitudeDelta")
val longitudeDelta = limitRegion.getDouble("longitudeDelta")
map.setMapStatusLimits(LatLngBounds(
LatLng(latitude - latitudeDelta, longitude - longitudeDelta),
LatLng(latitude + latitudeDelta, longitude + longitudeDelta)
))
}
}
......@@ -165,7 +165,7 @@ internal class AMapViewManager : ViewGroupManager<AMapView>() {
@ReactProp(name = "limitRegion")
fun setLimitRegion(view: AMapView, limitRegion: ReadableMap) {
view.map.setMapStatusLimits(AMapConverter.LatLngBounds(limitRegion))
view.setLimitRegion(limitRegion)
}
@ReactProp(name = "tilt")
......
......@@ -97,12 +97,12 @@ class MapView extends Component {
coordinate: LatLng,
/**
* 设置可见地图区域的矩形
* 限制地图只能显示某个矩形区域
*/
limitRegion: Region,
/**
* 设置倾斜角度,取值范围 [0, 60]
* 倾斜角度,取值范围 [0, 60]
*/
tilt: PropTypes.number,
......
......@@ -5,19 +5,12 @@ const LatLng = PropTypes.shape({
longitude: PropTypes.number.isRequired,
})
const Span = PropTypes.shape({
const Region = PropTypes.shape({
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
latitudeDelta: PropTypes.number.isRequired,
longitudeDelta: PropTypes.number.isRequired,
})
const Region = PropTypes.shape({
center: LatLng,
span: Span,
})
export {
LatLng,
Span,
Region,
}
export {LatLng, Region}
#import <MAMapKit/MAMapView.h>
#import <MAMapKit/MAGeometry.h>
#import <React/RCTConvert.h>
#import <React/RCTConvert+CoreLocation.h>
#import "Coordinate.h"
......@@ -21,30 +20,15 @@ RCT_ENUM_CONVERTER(MAPinAnnotationColor, (@{
}), MAPinAnnotationColorRed, integerValue)
+ (Coordinate *)Coordinate:(id)json {
return [[Coordinate alloc] initWithCoordinate: [self CLLocationCoordinate2D:json]];
}
+ (MACoordinateSpan)MACoordinateSpan:(id)json {
json = [self NSDictionary:json];
return (MACoordinateSpan){
[self CLLocationDegrees:json[@"latitudeDelta"]],
[self CLLocationDegrees:json[@"longitudeDelta"]]
};
return [[Coordinate alloc] initWithCoordinate:[self CLLocationCoordinate2D:json]];
}
+ (MACoordinateRegion)MACoordinateRegion:(id)json {
return (MACoordinateRegion){
[self CLLocationCoordinate2D:json[@"center"]],
[self MACoordinateSpan:json[@"span"]]
};
}
+ (MAMapStatus *)MAMapStatus:(id)json {
return [MAMapStatus statusWithCenterCoordinate:(json[@"centerCoordinate"] ? [self CLLocationCoordinate2D:json[@"centerCoordinate"]] : (CLLocationCoordinate2D){-1.0f, -1.0f})
zoomLevel:json[@"zoomLevel"] ? [self CGFloat:json[@"zoomLevel"]] : -1
rotationDegree:json[@"rotationDegree"] ? [self CGFloat:json[@"rotationDegree"]] : -1
cameraDegree:json[@"cameraDegree"] ? [self CGFloat:json[@"cameraDegree"]] : -1
screenAnchor:json[@"screenAnchor"] ? [self CGPoint:json[@"screenAnchor"]] : CGPointMake(-1, -1)];
return MACoordinateRegionMake(
[self CLLocationCoordinate2D:json],
MACoordinateSpanMake(
[self CLLocationDegrees:json[@"latitudeDelta"]],
[self CLLocationDegrees:json[@"longitudeDelta"]]));
}
RCT_ARRAY_CONVERTER(Coordinate)
......
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