在刀片中,我需要在地图上显示用户的位置。我在不同的刀片服务器上使用了相同的js函数。在我的另一个刀片(配置文件)中,地图可以正常运行,但是在这里它进入了.fail()
UserController:
public function getMap(){
$user = User::user();
return json_encode($user);
}
刀片:
<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::get('getMap/{id}', 'UserController@getMap');
我收到错误404找不到
答案 0 :(得分:1)
您通过路由将用户ID传递给控制器。因此,您需要在控制器的$id
方法中添加参数getMap($id)
。然后,您可以$user = User::find($id)