gpt4 book ai didi

javascript - 如何将地址与 Google Map 动态集成?

转载 作者:行者123 更新时间:2023-11-28 01:36:32 25 4
gpt4 key购买 nike

我在我的应用程序上使用 PHP Laravel 4.0。

这是我的一个用户地址的示例:

img

最后,在 Location 部分,我将它们作为地址格式并排放置。

此外,我想使用该地址并将其与 Google map 集成。

我试过什么?

地址是 410 Walker Street Lowell MA 01851

我使用这个网站。 http://www.gpsvisualizer.com/geocode

为了获取它的位置,我得到了这个

42.635507, -71.328928

然后我像这样将它们包含在我的 HTML 中我从 https://developers.google.com/maps/tutorials/fundamentals/adding-a-google-map 得到的

<!DOCTYPE html>
<html>
<head>
<style>
#map-canvas {
width: 500px;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapCanvas = document.getElementById('map-canvas');
var mapOptions = {
center: new google.maps.LatLng(42.635507, -71.328928),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>

问题

因为我的应用程序有很多用户。我很难手动完成 - 一个接一个。我想让它动态生成到我应用程序中所有页面上的所有地址。

  • 根据我的尝试,有人可以帮助我或给我一点提示吗?
  • 你能告诉我我错过了什么吗?
  • 这背后的逻辑是什么?
  • 我需要为此下载特定的软件包或插件吗?

非常感谢参与这篇文章的每个人!!!

最佳答案

您可以使用 Google map 地理编码服务来解码您的地址:

https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple?hl=pt-br

var geocoder = new google.maps.Geocoder();

function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latlng = results[0].geometry.location; // this is what you want
alert(latlng)
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}

有了 LatLng 值,您可以使用它来使 map 居中

map.setCenter(latlng);

甚至画一个标记

  var marker = new google.maps.Marker({
map: map,
position: latlng
});

但深入研究您的样本。您可能只需要插入一个嵌入的 iframe,指向您从 PHP 获得的地址。首先你需要生成一个 API Key然后将其用作下面的示例:

<iframe
width="600"
height="450"
frameborder="0" style="border:0"
src="https://www.google.com/maps/embed/v1/place?key=YOUR_API_KEY
&q=<? echo "YOUR ADDRESS FROM PHP"?>
&attribution_source=Google+Maps+Embed+API">
</iframe>

关于javascript - 如何将地址与 Google Map 动态集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27804014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com