Skip to content

Commit

Permalink
v2.1.1: adds support for cameraZoomRange and cameraBoundary
Browse files Browse the repository at this point in the history
  • Loading branch information
stevennoad committed Jul 10, 2024
1 parent 4e6423f commit 3f2f7b5
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 250 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,26 @@ You can customise the map further by including a `settings` object within your s

```html
"settings": {
"colorScheme": "light",
"cameraDistance": 1000,
"isZoomEnabled": true,
"isScrollEnabled": true,
"isRotationEnabled": true,
"center": {
"latitude": 37.3318,
"longitude": -122.0312
}
"colorScheme": "light",
"cameraDistance": 1000,
"isZoomEnabled": true,
"isScrollEnabled": true,
"isRotationEnabled": true,
"style": "compact_callout",
"center": {
"latitude": 37.3318,
"longitude": -122.0312
},
"cameraZoomRange": {
"min": 5000,
"max": 10000
},
"cameraBoundary": {
"latitude": 37.3349,
"longitude": -122.0090,
"spanLatitude": 0.05,
"spanLongitude": 0.05
}
}
```

Expand Down Expand Up @@ -155,4 +166,4 @@ You can customise the map further by including a `settings` object within your s
}
}
[/apple_map]
```
```
2 changes: 1 addition & 1 deletion apple-maps.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Apple Maps
Plugin URI: https://github.com/stevennoad/apple-maps-plugin
Description: A plugin to add Apple Maps with multiple locations.
Version: 2.1.0
Version: 2.1.1
Author: Steve Noad
License: MIT
Text Domain: apple-maps
Expand Down
193 changes: 101 additions & 92 deletions assets/js/places.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,115 @@
document.addEventListener('DOMContentLoaded', () => {
const mapContainers = document.querySelectorAll('.apple-map-container');

if (mapContainers.length > 0) {
mapkit.init({
authorizationCallback: function(done) {
done(appleMapsSettings.token);
}
});

// Create a single map instance
const mapContainer = mapContainers[0];

// Extract and parse locations data
const locationsData = mapContainer.dataset.landmarks;
let locations = [];
try {
locations = JSON.parse(locationsData).locations;
} catch (error) {
console.error('Error parsing locations data:', error);
return;
}

// Settings for the map
const settings = appleMapsSettings.settings;

const getColorScheme = colorScheme => {
switch (colorScheme) {
case 'light':
return mapkit.Map.ColorSchemes.Light;
case 'dark':
return mapkit.Map.ColorSchemes.Dark;
case 'hybrid':
return mapkit.Map.ColorSchemes.Hybrid;
default:
return mapkit.Map.ColorSchemes.Light;
}
};

const mapOptions = {
colorScheme: getColorScheme(settings.colorScheme),
isZoomEnabled: settings.isZoomEnabled ?? true,
isScrollEnabled: settings.isScrollEnabled ?? true,
isRotationEnabled: settings.isRotationEnabled ?? true,
};

const map = new mapkit.Map(mapContainer, mapOptions);

const annotations = [];
let pendingLookups = locations.length;

locations.forEach(location => {
const id = location.placeId;
const glyphText = appleMapsSettings.glyph || '';

const lookup = new mapkit.PlaceLookup();
lookup.getPlace(id, (error, place) => {
pendingLookups--;

if (error) {
console.error('Error looking up place:', error);
} else {
const annotation = new mapkit.PlaceAnnotation(place);
annotation.glyphText = glyphText;
map.addAnnotation(annotation);
annotations.push(annotation);

const getPlaceDetailsStyle = style => {
const mapContainers = document.querySelectorAll('.apple-map-container');

if (mapContainers.length > 0) {
mapkit.init({
authorizationCallback: function(done) {
done(appleMapsSettings.token);
}
});

// Create a single map instance
const mapContainer = mapContainers[0];

// Extract and parse locations data
const locationsData = mapContainer.dataset.landmarks;
let locations = [];
try {
locations = JSON.parse(locationsData).locations;
} catch (error) {
console.error('Error parsing locations data:', error);
return;
}

// Settings for the map
const settings = appleMapsSettings.settings;

const getColorScheme = (colorScheme) => {
switch (colorScheme) {
case 'light':
return mapkit.Map.ColorSchemes.Light;
case 'dark':
return mapkit.Map.ColorSchemes.Dark;
case 'hybrid':
return mapkit.Map.ColorSchemes.Hybrid;
default:
return mapkit.Map.ColorSchemes.Light;
}
};

const mapOptions = {
colorScheme: getColorScheme(settings.colorScheme),
isZoomEnabled: settings.isZoomEnabled ?? true,
isScrollEnabled: settings.isScrollEnabled ?? true,
isRotationEnabled: settings.isRotationEnabled ?? true,
cameraZoomRange: new mapkit.CameraZoomRange(settings.cameraZoomRange?.min, settings.cameraZoomRange?.max)
};

const map = new mapkit.Map(mapContainer, mapOptions);

const annotations = [];
let pendingLookups = locations.length;

locations.forEach((location) => {
const id = location.placeId;
const glyphText = appleMapsSettings.glyph || '';

const lookup = new mapkit.PlaceLookup();
lookup.getPlace(id, (error, place) => {
pendingLookups--;

if (error) {
console.error('Error looking up place:', error);
} else {
const annotation = new mapkit.PlaceAnnotation(place);
annotation.glyphText = glyphText;
map.addAnnotation(annotation);
annotations.push(annotation);

const getPlaceDetailsStyle = (style) => {
switch (style) {
case 'automatic':
return mapkit.PlaceSelectionAccessory.Styles.Automatic
return mapkit.PlaceSelectionAccessory.Styles.Automatic;
case 'callout':
return mapkit.PlaceSelectionAccessory.Styles.Callout
return mapkit.PlaceSelectionAccessory.Styles.Callout;
case 'full_callout':
return mapkit.PlaceSelectionAccessory.Styles.FullCallout
return mapkit.PlaceSelectionAccessory.Styles.FullCallout;
case 'compact_callout':
return mapkit.PlaceSelectionAccessory.Styles.CompactCallout
return mapkit.PlaceSelectionAccessory.Styles.CompactCallout;
case 'open_in_maps':
return mapkit.PlaceSelectionAccessory.Styles.OpenInMaps
return mapkit.PlaceSelectionAccessory.Styles.OpenInMaps;
default:
return mapkit.PlaceSelectionAccessory.Styles.Automatic
return mapkit.PlaceSelectionAccessory.Styles.Automatic;
}
};

const accessory = new mapkit.PlaceSelectionAccessory({
const accessory = new mapkit.PlaceSelectionAccessory({
style: getPlaceDetailsStyle(settings.style),
});
annotation.selectionAccessory = accessory;
}

if (pendingLookups === 0) {
map.showItems(annotations);

if (settings.center?.latitude !== undefined && settings.center?.longitude !== undefined) {
map.center = new mapkit.Coordinate(settings.center.latitude, settings.center.longitude);
}

// Set camera distance if provided in settings
if (settings.cameraDistance !== undefined) {
map.cameraDistance = settings.cameraDistance;
}
}
});
});
}
annotation.selectionAccessory = accessory;
}

if (pendingLookups === 0) {
map.showItems(annotations);

if (settings.cameraBoundary?.latitude !== undefined && settings.cameraBoundary?.longitude !== undefined && settings.cameraBoundary?.spanLatitude !== undefined && settings.cameraBoundary?.spanLongitude !== undefined) {
const cameraBoundary = new mapkit.CoordinateRegion(
new mapkit.Coordinate(settings.cameraBoundary.latitude, settings.cameraBoundary.longitude),
new mapkit.CoordinateSpan(settings.cameraBoundary.spanLatitude, settings.cameraBoundary.spanLongitude)
);

map.cameraBoundary = cameraBoundary.toMapRect();
}

if (settings.center?.latitude !== undefined && settings.center?.longitude !== undefined) {
map.center = new mapkit.Coordinate(settings.center.latitude, settings.center.longitude);
}

if (settings.cameraDistance !== undefined) {
map.cameraDistance = settings.cameraDistance;
}
}
});
});
}
});
Loading

0 comments on commit 3f2f7b5

Please sign in to comment.