Skip to content

Instantly share code, notes, and snippets.

@ReallyBean
Created June 19, 2018 09:29
Show Gist options
  • Save ReallyBean/244d1755cbebd173f30dabc2be34ed37 to your computer and use it in GitHub Desktop.
Save ReallyBean/244d1755cbebd173f30dabc2be34ed37 to your computer and use it in GitHub Desktop.
How to show the multiple locations from address using Google Map with laravel
@extends('layouts.app')
@section('content')
<div class="container">
<div id="google-map"></div>
</div>
@endsection
@section('script')
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<script src="http://momentjs.com/downloads/moment.js"></script>
<script type="text/javascript">
json_locations = {!! json_encode($events) !!};
var events = json_locations.data;
var geocoder;
var map;
var bounds = new google.maps.LatLngBounds();
function initialize() {
map = new google.maps.Map(document.getElementById("google-map"), {
// zoom: 3,
center: {lat: -28.024, lng: 140.887}
});
geocoder = new google.maps.Geocoder();
for (i = 0; i < events.length; i++) {
geocodeAddress(events, i);
}
}
google.maps.event.addDomListener(window, "load", initialize);
function geocodeAddress(events, i) {
geocoder.geocode( {'address': events[i].event_location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
icon: 'http://maps.google.com/mapfiles/ms/icons/blue.png',
map: map,
position: results[0].geometry.location,
animation: google.maps.Animation.DROP,
});
infoWindow(marker, map, events[i]);
bounds.extend(marker.getPosition());
map.fitBounds(bounds);
} else {
alert("geocode of " + address + " failed:" + status);
}
});
}
function infoWindow(marker, map, event) {
start_time = moment(event.event_start_datetime).format("YYYY/MM/DD(ddd) hh:mm");
end_time = moment(event.event_end_datetime).format("YYYY/MM/DD(ddd) hh:mm");
var title = start_time + ' - ' + end_time;
var address = event.event_location;
var url = {!! json_encode(route('index')) !!} + '/event/' + event.event_slug;
var url_name = event.event_name;
google.maps.event.addListener(marker, 'click', function() {
var html = "<div><p>" + title + "</p><p><a href='" + url + "'>" + url_name + "</a></p><p>" + address + "</p></div>";
iw = new google.maps.InfoWindow({
content: html,
maxWidth: 300
});
iw.open(map, marker);
});
}
</script>
<!-- GOOGLE MAP SCRIPT -->
@endsection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment