如何更改Laravel中嵌入式OpenStreetMap的坐标?

发布于 2025-01-22 12:06:31 字数 1546 浏览 5 评论 0原文

我们正在尝试将地图嵌入到我们的网站中。对于固定地址,这正常工作,但我们希望用户在需要/需要的情况下可以更改后端/仪表板中的地址。因此,后端/仪表板具有填写地址的表格,提交后应更新地图。但是地图没有更新,当我们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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文