如何更改Laravel中嵌入式OpenStreetMap的坐标?
我们正在尝试将地图嵌入到我们的网站中。对于固定地址,这正常工作,但我们希望用户在需要/需要的情况下可以更改后端/仪表板中的地址。因此,后端/仪表板具有填写地址的表格,提交后应更新地图。但是地图没有更新,当我们dd时,返回为null。
我们正在使用OpenStreetMap,因为我们不想为GoogleMaps API付费。
以下是我们的代码:
public function update(Request $request, Contact $contact)
{
$validated = $request->validate([
'phone' => 'required',
'mobile' => 'required',
]);
$contact->building = $request->building;
$contact->street = $request->street;
$contact->city = $request->city;
$contact->postcode = $request->postcode;
$contact->phone = $request->phone;
$contact->mobile = $request->mobile;
$search_url = "https://nominatim.openstreetmap.org/search?q=$contact->buiding+".str_replace(' ', '+', $contact->street).",+$contact->city";
$httpOptions = [
"http" => [
"method" => "GET",
"header" => "User-Agent: Nominatim-Test"
]
];
$streamContext = stream_context_create($httpOptions);
$json = file_get_contents($search_url, false, $streamContext);
$decoded = json_decode($json, true);
$lat = $decoded[0]["lat"];
$lng = $decoded[0]["lon"];
$contact->longitude = $lng;
$contact->latitude = $lat;
$contact->save();
return redirect()->route('contact.index')->with("message", "Successfully updated.");
}
We are trying to embed a map into our website. This works fine for a fixed address but we want the user to be able to change the address in the backend/dashboard if they want/need to. So the backend/dashboard has a form to fill in the address and it should update the map once submitted. But the map doesn't update and when we dd the return was null.
We are using OpenStreetMap because we do not wish to pay for GoogleMaps API.
Below is our code:
public function update(Request $request, Contact $contact)
{
$validated = $request->validate([
'phone' => 'required',
'mobile' => 'required',
]);
$contact->building = $request->building;
$contact->street = $request->street;
$contact->city = $request->city;
$contact->postcode = $request->postcode;
$contact->phone = $request->phone;
$contact->mobile = $request->mobile;
$search_url = "https://nominatim.openstreetmap.org/search?q=$contact->buiding+".str_replace(' ', '+', $contact->street).",+$contact->city";
$httpOptions = [
"http" => [
"method" => "GET",
"header" => "User-Agent: Nominatim-Test"
]
];
$streamContext = stream_context_create($httpOptions);
$json = file_get_contents($search_url, false, $streamContext);
$decoded = json_decode($json, true);
$lat = $decoded[0]["lat"];
$lng = $decoded[0]["lon"];
$contact->longitude = $lng;
$contact->latitude = $lat;
$contact->save();
return redirect()->route('contact.index')->with("message", "Successfully updated.");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论