Ad

Laravel - Trying To Show Google Map Based On User Id

In my blade I need to show a user's location on a map. I'm using the same js function in different blade. In my other blade(profile) the map works fine but here it goes into .fail()

UserController:

public function getMap(){
        $user = User::user();
        return json_encode($user);
    }

Blade:

<div id="address-map-container" class="form-group row" style="width:100%;height:400px; ">
        <div style="width: 100%; height: 100%" id="map">
        </div>
    </div>
@endsection

@section('scripts')

 <script async defer src="https://maps.googleapis.com/maps/api/js?key=[MY_KEY]&libraries=places"></script>

    <script src="/js/circles.js"></script>
    <script>
    var site_url = window.location.pathname;
    var id = site_url.substring(site_url.lastIndexOf('/'));
    $(() =>{
        circleInitialize('getMap'+id)

    })
</script>
 @endsection

circles.js:

$.ajaxSetup({
  headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
})
function circleInitialize(url) {
  $.ajax({
    dataType: "json",
    url: url,
    type: 'GET',
  })
    .done((data) => {
      var distance = data.distance*1000;
      var latitude = data.address_latitude;
      var longitude = data.address_longitude;
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 12,
        center: {lat: latitude, lng: longitude},
        mapTypeId: 'roadmap'
      });
      var center = new google.maps.LatLng(latitude, longitude);
      var circle = new google.maps.Circle({
        center: center,
        map: map,
        radius: distance,
        fillColor: '#4CCC60',
        fillOpacity: 0.3,
        strokeColor: "#FFF",
        strokeWeight: 0
      });
    })
    .fail(() => {
      alert("error circle.js")    
    }) 
}

Route:

Route::get('getMap/{id}', '[email protected]');

I get an error 404 Not found

Ad

Answer

Your passing the users id to the controller through the route. So you need to add the parameter $id in your getMap($id) method in your controller. Then you can do $user = User::find($id)

Ad
source: stackoverflow.com
Ad