I am trying to show markers on Google map from already stored values (lattitude and longitude) in mysql databse. but when i try to load it markers are not showing in google map. Am new to laravel. please help me to implement it.
Following is the code in view page
@extends('user.layout.app') @section('content') <script src="{{ url('js/user/location.js') }}"></script> <div class="container-fluid add-location"> <div class="row"> <div class="col-md-12"> <div class="card"> <form method="post" action="" name="clinicssview" id="clinicssview" enctype="multipart/form-data" novalidate> {{ csrf_field() }} <div class="card-header"> <h4 class="card-title"> View Clinics </h4> </div> @if(!empty($errors->all())) <div class="row"> @foreach ($errors->all() as $error) <div class="col-lg-12"> <div class="alert alert-danger"> <span>{{ $error }}</span> </div> </div> @endforeach </div> @endif <div class="card-content"> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-6"> <div class="form-group hide"> <label class="control-label">Location <star>*</star> </label> <input id="pac-input" name="location" class="controls form-control" type="text" placeholder="Search Box"> <!-- <div id="map" height="1000" width="1000"></div> --> </div> <h4 >Preview</h4> <div class="form-group"> <div id="regularMap" class="map"></div> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Clinic Name <star>*</star> </label> <input disabled id="user_name" name="user_name" class="controls form-control" type="text" placeholder="Clinc Name" value="{{$clinics->clinicName}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Contact First Name <star>*</star> </label> <input disabled id="contact_fname" name="contact_fname" class="controls form-control" type="text" placeholder="Contact First Name" value="{{$clinics->clinicFname}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Contact Second Name <star>*</star> </label> <input disabled id="contact_sname" name="contact_sname" class="controls form-control" type="text" placeholder="Contact Second Name" value="{{$clinics->clinicLname}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Contact Address <star>*</star> </label> <input disabled id="contact_address" name="contact_adress" class="controls form-control" type="text" placeholder="Contact Address" value="{{$clinics->clinicAddress}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Contact City <star>*</star> </label> <input disabled id="contact_city" name="contact_city" class="controls form-control" type="text" placeholder="City" value="{{$clinics->clinicCity}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Contact State <star>*</star> </label> <input disabled id="contact_state" name="contact_state" class="controls form-control" type="text" placeholder="State" value="{{$clinics->clinicState}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Clinic Zip <star>*</star> </label> <input disabled id="zip" name="zip" class="controls form-control" type="text" placeholder="Zip" value="{{$clinics->clinicZip}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> <label for="email" class="control-label">Email <star>*</star> </label> <input disabled id="email" type="email" class="form-control" name="email" placeholder="Email" value="{{$clinics->clinicEmail}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Clinic Phone Number <star>*</star> </label> <input disabled id="phone" name="phone" class="controls form-control" type="text" placeholder="Phone Number" value="{{$clinics->clinicPhone}}"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <label class="control-label">Clinic Website <star>*</star> </label> <input disabled id="clinic_website" name="clinic_website" class="controls form-control" type="text" placeholder="Website" value="{{$clinics->clinicURL}}"> </div> </div> </div> <div class="row"> </form> </div> </div> </div> </div> <script> var customLabel = { restaurant: { label: 'R' }, bar: { label: 'B' } }; function initMap() { var map = new google.maps.Map(document.getElementById('regularMap'), { center: new google.maps.LatLng(-33.863276, 151.207977), zoom: 12 }); var infoWindow = new google.maps.InfoWindow; // Change this depending on the name of your PHP or XML file downloadUrl('https://storage.googleapis.com/mapsdevsite/json/mapmarkers2.xml', function(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName('marker'); Array.prototype.forEach.call(markers, function(markerElem) { var id = markerElem.getAttribute('id'); var name = markerElem.getAttribute('name'); var address = markerElem.getAttribute('address'); var type = markerElem.getAttribute('type'); var point = new google.maps.LatLng( parseFloat(markerElem.getAttribute('lat')), parseFloat(markerElem.getAttribute('lng'))); var infowincontent = document.createElement('div'); var strong = document.createElement('strong'); strong.textContent = name infowincontent.appendChild(strong); infowincontent.appendChild(document.createElement('br')); var text = document.createElement('text'); text.textContent = address infowincontent.appendChild(text); var icon = customLabel[type] || {}; var marker = new google.maps.Marker({ map: map, position: point, label: icon.label }); marker.addListener('click', function() { infoWindow.setContent(infowincontent); infoWindow.open(map, marker); }); }); }); } function downloadUrl(url, callback) { var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest; request.onreadystatechange = function() { if (request.readyState == 4) { request.onreadystatechange = doNothing; callback(request, request.status); } }; request.open('GET', url, true); request.send(null); } function doNothing() {} </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap"> </script> @endsection
I will get latitude and longitude values by <?php echo $loc->lat ?> & <?php echo $loc->long ?>
in this same page but i dont know how to pass it, please help me
Advertisement
Answer
Since your script is inside the Laravel blade, you can assign PHP variable longitude and latitude to a javascript variable.
//Suppose your PHP variable name is $loc having lat and long attribute <script> let provided_longitude = <?php echo $loc->long; ?>; let provided_latitude = <?php echo $loc->lat; ?>; console.log(provided_longitude, provided_latitude); </script>
Now you can use provided_longitude and provided_latitude into google map.
Iframe Example inside the example.blade.php
<iframe width="600" height="450" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=YOUR_API_KEY &q={{$loc->lat}},{{$loc->long}}" allowfullscreen> </iframe>
EDIT 1
To show the markers from the table (eg: orders having column longitude, latitude)
//Inside Your Controller
$orders = Order::select('id', 'longitude', 'latitude'); return view('your.page', [ 'orders' => $orders ]);
//Inside your/page.blade.php
<div id="map" style="width: 500px; height: 400px;"></div> ... <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> <script type="text/javascript"> var locations = <?php echo $orders ?>; var map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: new google.maps.LatLng(-33.92, 151.25), mapTypeId: google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i]['latitude'], locations[i]['longitude']), map: map }); } </script>