开发者

why doesn't this php work with google maps?

开发者 https://www.devze.com 2023-04-05 06:27 出处:网络
I\'m trying to figure out why this PHP script isn\'t creating the map I specify in the HTML. any ideas?

I'm trying to figure out why this PHP script isn't creating the map I specify in the HTML. any ideas?

<!doctype html>

<html>
    <head>

        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

        <style>

            html { height: 100% }
            body { height: 100%; margin: 0; padding: 0 }
            #map { height: 100% }

        </style>

        <script
        src="http://maps.googleapis.com/maps/api/js?sensor=false">

        </script>

        <?php
            $lat = $_POST['lat'];
            $long = $_POST['long'];

            echo "

        <script>

            function callMap() {

                var latlng = new google.maps.LatLng($lat, $long);"; ?>
     开发者_C百科           var options = {
                    zoom: 5,
                    center: latlng,
                    mapTypeId = google.maps.MapTypeId.TERRAIN

                };
                var map = new google.maps.Map(document.getElementById("map"),
                options);


            }

        </script>
    </head>
    <body onload="callMap()">
    <div id="map"></div>




    </body>

</html>


Your options declaration syntax is messed up:

var options = {
    zoom: 5,
    center: latlng,
    mapTypeId = google.maps.MapTypeId.TERRAIN // ERROR
};

Should be

var options = {
    zoom: 5,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
};


You also missing a bracket on the google js link.


I don't use PHP so I can't tell if there's anything wrong with that code. If you removed the PHP and hardcoded lat/lon values, does the map work? Maybe one problem could be just what values the form is posting with the latitude and longitude. You're not doing any sort of validation to ensure they are within acceptable bounds, e.g. +90 to -90 for latitude and +180 to -180 for longitude.

0

精彩评论

暂无评论...
验证码 换一张
取 消