Google Maps - samuelgrant/fight-for-kidz GitHub Wiki
This application uses Google Maps library alexpechkarev/google-maps on the index page to display the location of the event
Offical Documentation: Google Maps, Geo Coding | alexpechkarev/google-maps
- Go to https://developers.google.com/maps/documentation/javascript/get-api-key with your google account.
- Create a new API by following the guide and register it to the appropriate url
- Setup the GOOGLE_MAPS_KEY section of your .env file so it looks like this:
GOOGLE_MAPS_KEY={API key FROM GOOGLE}
We recommend keeping the geocode lookup in it's own function. This can be a static helper or part of a normal class.
The geocode lookup takes a string that can be either a mailing address or the name of a well known building. For example 230 Tay street or SIT Invercargill.
public function getGPS($address){
$response = \GoogleMaps::load('geocoding')
->setParam (['address' => $address])->get();
$json = json_decode($response, TRUE);
return ('lat: '.$json['results'][0]['geometry']['location']['lat'].", lng: ".$json['results'][0]['geometry']['location']['lng']);
}
The map uses the venue_gps field to get the correct location to be displayed on the map
<div id="map" style="width:100%; height: 450px; border:0"></div>
<script>
function initMap() {
var uluru = { {{$event->venue_gps}} };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key={{env('GOOGLE_MAPS_KEY')}}&callback=initMap">
</script>