Skip to content

Instantly share code, notes, and snippets.

@wedwin53
Last active November 23, 2018 20:00
Show Gist options
  • Save wedwin53/154a4e67a2fafb0b566c27e8f7db4a1d to your computer and use it in GitHub Desktop.
Save wedwin53/154a4e67a2fafb0b566c27e8f7db4a1d to your computer and use it in GitHub Desktop.
API GOOGLE MAPS
// LOCATIONS
var API_KEY = 'YOUR_API_KEY'
var API_KEY2 = 'YOUR_API_KEY2'// ONLY ONE NEEDED
var url = 'https://www.googleapis.com/geolocation/v1/geolocate?key='+API_KEY2;
var config = {
method: 'POST'
}
fetch(url, config)
.then(res => res.json())
.then(data => console.log(data.location.lat + ' - ' +data.location.lng, 'location'))
.catch(err => console.log(err))
//SNAPS TO ROAD
var PATH = '60.170880,24.942795|60.170879,24.942796|60.170877,24.942796'
var url2 = 'https://roads.googleapis.com/v1/snapToRoads?key='+API_KEY2+'&path='+PATH;
fetch(url2)
.then(res => res.json())
.then(data => console.log(data, 'SNAPS TO ROAD'))
.catch(err => console.log(err))
//TEXT SEARCH
var cors_url = 'https://cors-anywhere.herokuapp.com/'
var url_textsearch = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=123+main+street&key='+API_KEY2;
fetch(cors_url+url_textsearch)
.then(res => res.json())
.then(data => console.log(data.results[0].name, 'TEXT SEARCH'))
.catch(err => console.log(err))
//geocode
var add = 'texas'
var url_geocode = 'https://maps.googleapis.com/maps/api/geocode/json?address='+add+'&key='+API_KEY2
fetch(url_geocode)
.then(res => res.json())
.then(data => console.log(data, 'geocode'))
.catch(err => console.log(err))
//directions
var url_directions = 'https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key='+API_KEY2
fetch(url_geocode)
.then(res => res.json())
.then(data => console.log(data, 'directions'))
.catch(err => console.log(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment