使用php的简单谷歌地图

时间:2014-09-02 21:50:58

标签: php google-maps google-maps-api-3

Web开发的新手,我想知道是否有一种简单的方法可以从表中绘制简单的纬度和经度坐标并在地图上渲染它们。我创建了一个简单的地方页面,它使用php从地点表中的每个存储位置绘制名称,地址等。我只是想创建一个非常简单的地图,在lat和lng坐标处显示标记。对不起,如果这是一个愚蠢的问题。

3 个答案:

答案 0 :(得分:2)

这不是PHP问题。它使用javascript。

这应该这样做:

<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
    var myMap;
    var myLatlng = new google.maps.LatLng(52.518903284520796,-1.450427753967233);
    function initialize() {
        var mapOptions = {
            zoom: 13,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP  ,
            scrollwheel: false
        }
        myMap = new google.maps.Map(document.getElementById('map'), mapOptions);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: myMap,
            title: 'Name Of Business',
            icon: 'http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png'
        });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

<div id="map" style="width:500px; height: 500px;">

</div>

只需将myLatLng变量更改为您想要的内容即可。

如果您需要更多帮助,请参阅以下文档: https://developers.google.com/maps/documentation/javascript/tutorial#HelloWorld

答案 1 :(得分:0)

这很简单。以下是Google Maps API的入门部分。您只需要注册一个API密钥,但它都写在那里。

https://developers.google.com/maps/documentation/javascript/tutorial

答案 2 :(得分:0)

这是一个例子

<?php
$url = "http://maps.google.com/maps/api/geocode/json?address=West+Bridgford&sensor=false&region=UK";
$response = file_get_contents($url);
$response = json_decode($response, true);

//print_r($response);

$lat = $response['results'][0]['geometry']['location']['lat'];
$long = $response['results'][0]['geometry']['location']['lng'];

echo "latitude: " . $lat . " longitude: " . $long;
?>