我在地图上用经纬度显示用户位置时遇到问题。在我托管/部署我的项目后,我注意到位置是错误的,它显示了一些我什至不知道可能是我的互联网提供商位置的地方。
我在我的 Laravel 项目中使用的是: -Leaflet.js -geoip
我的控制器我想在我的 create.blade 中显示地图
//user's location
private function getLocation(){
//return location coordinate from user's ip
return geoip()->getLocation($_SERVER['REMOTE_ADDR']);
}
//create
public function create($id, Request $request){
$location = $this->getLocation();
return view('presence.create', compact(
'location'
));
}
我对 $_SERVER['REMOTE_ADDR'] 行持怀疑态度,我不确定它显示的是真实用户的 ip 还是我的提供商的 ip
我的刀片中的 leaflet.js
<div id="map"></div>
<script>
var map = L.map('map').setView([-6.297979, 106.922899], 10);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
//put user's latitude and longitude to marker
L.marker([ {{ $location->lat }}, {{ $location->lon }}]).addTo(map)
.bindPopup('{{ auth()->user()->name }}')
.openPopup();
//destination marker
var destination = L.marker([ -6.297979, 106.922899]).addTo(map)
.bindPopup('Tujuan')
.openPopup();
destination._icon.classList.add("huechange");
var circle = L.circle([-6.297979, 106.922899], {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5,
radius: 20
}).addTo(map);
</script>