php 根据 GPS 坐标将 Google Map 嵌入 HTML 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2517736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embed Google Map into HTML page based on GPS coordinates
提问by ensnare
I have a PHP photo gallery that reads the GPS coordinates from images. I'd like to modify it to use the coordinates and include a google map on the photo page. Is there a simple way to display a google map on an HTML page by supplying just this pair of information?
我有一个 PHP 照片库,可以从图像中读取 GPS 坐标。我想修改它以使用坐标并在照片页面上包含一个谷歌地图。是否有一种简单的方法可以通过仅提供这对信息在 HTML 页面上显示谷歌地图?
Thanks.
谢谢。
回答by Daniel Vassallo
The following are a few examples that you may help you getting started:
以下是一些可以帮助您入门的示例:
Using the Google Maps API v2:
使用Google Maps API v2:
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false"
type="text/javascript"></script>
</head>
<body onunload="GUnload()">
<div id="map" style="width: 400px; height: 300px"></div>
<script type="text/javascript">
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(51.49, -0.12), 8);
</script>
</body>
</html>
You simply need to change the latitude and longitude in the GMap2.setCenter()method. The last paramater is the zoom level.
您只需要更改方法中的纬度和经度GMap2.setCenter()。最后一个参数是缩放级别。
Using the Google Maps API v3:
使用Google Maps API v3:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>
<body>
<div id="map" style="width: 400px; height: 300px"></div>
<script type="text/javascript">
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(51.49, -0.12),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
</script>
</body>
</html>
When using version 3 of the Maps API, you would need to pass your parameters as options to the google.maps.Map()constructor. The above example should be self explanatory.
使用 Maps API 的第 3 版时,您需要将参数作为选项传递给google.maps.Map()构造函数。上面的例子应该是不言自明的。
Using the Static Map API:
使用静态地图 API:
<img src="https://maps.google.com/maps/api/staticmap?center=51.49,-0.12&zoom=8&size=400x300&sensor=false" style="width: 400px; height: 400px;" />
The Static Map API, as luca suggestedmight be a very good idea.
Simply pass the the latitude and longitude paramaters in an image tag, as in the example above. It would display the map below, directly rendered from Google:
只需在图像标签中传递纬度和经度参数,如上例所示。它会显示下面的地图,直接从谷歌渲染:


回答by Luca Rocchi
use static google map you have to supply just some other addition info
使用静态谷歌地图你只需要提供一些其他的附加信息
http://maps.google.com/maps/api/staticmap?center=51.477222,0&zoom=14&size=400x400&sensor=false
http://maps.google.com/maps/api/staticmap?center=51.477222,0&zoom=14&size=400x400&sensor=false
it only show an image of the map
它只显示地图的图像
here is a link to the API
这是 API 的链接
https://developers.google.com/maps/documentation/maps-static/intro
https://developers.google.com/maps/documentation/maps-static/intro
there is a lot of other options if needed
如果需要,还有很多其他选择
回答by streetlogics
Since you're using PHP, you could also use the PHP Google Map API class, which has also just been updated to use V3 of the Google Maps JS APIs.
由于您使用的是 PHP,您还可以使用PHP Google Map API 类,该类也刚刚更新为使用 Google Maps JS API 的 V3。

