Last active
August 19, 2022 08:44
-
-
Save yitznewton/4f096a8c5fc1e6bd08c7 to your computer and use it in GitHub Desktop.
Starbucks Stores API / Google Maps JS API mashup with browser geolocation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head></head> | |
<body> | |
<div id="map-canvas" style="height: 600px; width: 600px;"></div> | |
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_GOOGLE_MAPS_API_KEY_HERE"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
<script> | |
var positionToStarbucksLatlng = function(position) { | |
return position.coords.latitude + ',' + position.coords.longitude; | |
}; | |
var positionToGoogleLatLng = function(position) { | |
return new google.maps.LatLng( | |
position.coords.latitude, | |
position.coords.longitude | |
); | |
}; | |
var storeToGoogleLatLng = function(store) { | |
return new google.maps.LatLng( | |
store.store.coordinates.latitude, | |
store.store.coordinates.longitude | |
); | |
}; | |
var drawStoresMap = function(position, storesData) { | |
var map = new google.maps.Map(document.getElementById('map-canvas'), { | |
center: positionToGoogleLatLng(position), | |
zoom: 12 | |
}); | |
for (var i = 0; i < storesData.stores.length; i++) { | |
drawMarker(map, storesData.stores[i]); | |
} | |
}; | |
var drawMarker = function(map, store) { | |
var marker = new google.maps.Marker({ | |
map: map, | |
position: storeToGoogleLatLng(store), | |
title: store.store.name | |
}); | |
}; | |
$(document).ready(function() { | |
navigator.geolocation.getCurrentPosition(function(position) { | |
$.ajax({ | |
url: 'https://testhost.openapi.starbucks.com/location/v2/stores/nearby', | |
headers: { | |
'Accept': 'application/json' | |
}, | |
data: { | |
radius: 10, | |
latlng: positionToStarbucksLatlng(position) | |
}, | |
success: function(storesData) { | |
drawStoresMap(position, storesData); | |
} | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Nice tutorial, going to give this a go tomorrow and see what else I can add and mash into it. I'm wondering though, the Starbucks API seems to work together nicely with Google Maps API, would it be as simple to use something else like a weather API let's say, to connect with Google maps? thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the code for my Google Maps JavaScript API mashup tutorial