Skip to content

Instantly share code, notes, and snippets.

@mtngit14
Forked from yitznewton/starbucks_map.html
Created November 2, 2016 18:19
Show Gist options
  • Save mtngit14/d0979419e88e78d0093ce8b4e05b0fd9 to your computer and use it in GitHub Desktop.
Save mtngit14/d0979419e88e78d0093ce8b4e05b0fd9 to your computer and use it in GitHub Desktop.
Starbucks Stores API / Google Maps JS API mashup with browser geolocation
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment