Newer
Older
class AppleMaps extends Component {
const { token, children, initialMapType } = this.props
this.canvas.id = 'currentLocationOverride'
mapkit.init({
authorizationCallback: function(done) {
done(token)
}
})
this.map = new mapkit.Map('map')

Zandor Smith
committed
this.annotations = {};
// Set initial mapType
if(initialMapType !== undefined) {
this.map.mapType = initialMapType
}
tonyduanesmith
committed
// Annotations
if (child.props.isAnnotation) {
tonyduanesmith
committed
this.createAnnotation(child.props)
} else if (children !== undefined && children.props) {
if (children.props.isAnnotation) {
tonyduanesmith
committed
this.createAnnotation(children.props)
}
}
// Image Annotations
if (children !== undefined && children.length) {
children.forEach(child => {
if (child.props.isImageAnnotation) {
this.createImageAnnotation(child.props)
}
})
} else if (children !== undefined && children.props) {
if (children.props.isImageAnnotation) {
this.createImageAnnotation(children.props)
}
}
tonyduanesmith
committed
// Current Location Override
if (children !== undefined && children.length) {
if (child.type.name === 'CurrentLocationOverride') {
tonyduanesmith
committed
this.createCurrentLocationOverride(child.props)
tonyduanesmith
committed
} else if (children !== undefined && children.props) {
if (children.type.name === 'CurrentLocationOverride') {
tonyduanesmith
committed
this.createCurrentLocationOverride(children.props)
}
tonyduanesmith
committed
// Set main coords
this.setMainCoords()

Zandor Smith
committed
const {
children,
latitude,
longitude,
spanLat,
spanLong,
height,
autoAdjust

Zandor Smith
committed
} = this.props

Zandor Smith
committed
prevProps.latitude !== latitude ||
prevProps.longitude !== longitude ||
prevProps.spanLat !== spanLat ||
prevProps.spanLong !== spanLong ||
prevProps.zoomLevel !== zoomLevel ||
prevProps.width !== width ||
prevProps.height !== height ||
prevProps.autoAdjust !== autoAdjust
) && autoAdjust) {

Zandor Smith
committed
this.setMainCoords()
}
if (children !== undefined && children.length) {
children.forEach(child => {
if (child.props.isAnnotation) {
this.updateAnnotation(child.props)
}
})
} else if (children !== undefined && children.props) {
if (children.props.isAnnotation) {
this.updateAnnotation(children.props)
}
}
let checkCurrentLocationLatitudeChange, checkCurrentLocationLongitudeChange, checkCurrentLocationDirectionChange
const firstChild = children[0] ? children[0] : children;
const prevFirstChild = prevProps.children[0] ? prevProps.children[0] : prevProps.children;

Zandor Smith
committed
checkCurrentLocationLatitudeChange =
firstChild.props.latitude !==
prevFirstChild.props.latitude

Zandor Smith
committed
checkCurrentLocationLongitudeChange =
firstChild.props.longitude !==
prevFirstChild.props.longitude
checkCurrentLocationDirectionChange =
firstChild.props.direction !==
prevFirstChild.props.direction

Zandor Smith
committed
checkCurrentLocationLatitudeChange ||
checkCurrentLocationLongitudeChange ||
checkCurrentLocationDirectionChange
tonyduanesmith
committed
if (children !== undefined && children.length) {
children.forEach(child => {
if (child.type.name === 'CurrentLocationOverride') {
this.updateCurrentLocationOverride(child.props)
tonyduanesmith
committed
}
})
} else if (children !== undefined && children.props) {
if (children.type.name === 'CurrentLocationOverride') {
this.updateCurrentLocationOverride(children.props)
tonyduanesmith
committed
}
}
tonyduanesmith
committed
tonyduanesmith
committed
const {

Zandor Smith
committed
id,
tonyduanesmith
committed
longitude,
latitude,
color,
glyphText,
tonyduanesmith
committed
selected,
title,
tonyduanesmith
committed
} = annotationOptions
let MarkerAnnotation = mapkit.MarkerAnnotation
let coords = new mapkit.Coordinate(latitude, longitude)
let newAnnotation = new MarkerAnnotation(coords, {
color,
title,
subtitle,
})
glyphText ? (newAnnotation.glyphText = glyphText) : ''
glyphImage ? (newAnnotation.glyphImage = { 1: glyphImage }) : ''

Zandor Smith
committed
if(id) {
this.annotations[id] = newAnnotation;
} else {
console.warn("Apple MapKitJS annotation created without id prop!");
}

Zandor Smith
committed
updateAnnotation(annotationOptions) {
const {
id,
latitude,
longitude
} = annotationOptions
if(id === undefined) {
return
}
if(!(id in this.annotations)) {
this.createAnnotation(annotationOptions)
return
}
let annotation = this.annotations[id];
if(latitude !== annotation.coordinate.latitude || longitude !== annotation.coordinate.longitude) {
annotation.coordinate = new mapkit.Coordinate(latitude, longitude)
}
}
createImageAnnotation(annotationOptions) {
const {
longitude,
latitude,
url,
selected,
title,
} = annotationOptions
let ImageAnnotation = mapkit.ImageAnnotation
let coords = new mapkit.Coordinate(latitude, longitude)
let newAnnotation = new ImageAnnotation(coords, {
title,
subtitle,
selected,
tonyduanesmith
committed
this.map.showItems([newAnnotation])
}
createCurrentLocationOverride(locationOptions) {
tonyduanesmith
committed
const { longitude, latitude, direction } = locationOptions
// AppleMaps needs options structured this way
const options = {
data: {
direction: direction
}
}
const coordinate = new mapkit.Coordinate(latitude, longitude)
this.currentLocation = new mapkit.Annotation(
coordinate,
let ctx = this.canvas.getContext('2d')
ctx.beginPath()
ctx.translate(150, 135)
ctx.rotate((options.data.direction * Math.PI) / 180)
ctx.lineCap = 'round'
ctx.moveTo(0, 7)
ctx.lineTo(10, 12)
ctx.lineTo(0, -13)
ctx.lineTo(-10, 12)
ctx.lineTo(0, 7)
ctx.fillStyle = '#08F'
ctx.strokeStyle = '#08F'
ctx.stroke()
ctx.fill()
this.map.showItems([this.currentLocation])
}
updateCurrentLocationOverride(locationOptions) {
const { longitude, latitude } = locationOptions
const coordinate = new mapkit.Coordinate(latitude, longitude)
this.currentLocation.coordinate = coordinate
tonyduanesmith
committed
}
const { longitude, latitude, spanLat, spanLong } = this.props

Zandor Smith
committed
this.map.region = new mapkit.CoordinateRegion(
new mapkit.Coordinate(latitude, longitude),
new mapkit.CoordinateSpan(spanLat ? spanLat : this.zoomLevel(), spanLong ? spanLong : this.zoomLevel())
}
const { zoomLevel } = this.props
case 0:
return 300
case 1:
return 75
case 2:
return 18.75
case 3:
return 4.68
case 4:
return 1.17
case 5:
return 0.39
case 6:
return 0.073
case 7:
return 0.018
case 8:
return 0.0045
default:
return 0.35
}
}
const { width, height } = this.props
style={{
height: height
}}
}
AppleMaps.defaultProps = {
width: '100wh',
height: '100vh',
latitude: -1.5491,
autoAdjust: false
}