无法使用地理编码在线工作,但可以使用localhost

时间:2016-03-25 03:44:44

标签: php google-maps geocoding

这在localhost上很有用,但不能在线工作。我到处寻找解决方案。我尝试过用于服务器和浏览器的Google密钥。

当我将URL放在浏览器中时,我得到了lat和long的正确响应。然而,一旦我上传代码,它就不会拉扯lat和long。我可以手动放入lat和long,效果很好。任何帮助都会很棒。

"https://maps.google.com/maps/api/geocode/json?address=458+4th+st,+Atwater,+California,+95301&key=My_key"


<?php require_once("includes/connection.php");
            $id=$_GET['id'];
            $id=64;
            $query = "SELECT * FROM events WHERE id = '".$id."'";
            $result=mysql_query($query);
                while($row2 = mysql_fetch_assoc($result)){
                    $name=$row2['name'];
                    $add = $row2['address']." ".$row2['city']." ".$row2['state']." ".$row2['zip'];
                    }

        ?>


            <link media="all" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic" rel="stylesheet">

            <script type='text/javascript' src='assets/jquery.js'></script>
            <script type='text/javascript' src='assets/jquery-migrate.js'></script>

            <?php /* === GOOGLE MAP JAVASCRIPT NEEDED (JQUERY) ==== */ ?>
            <script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>
            <script type='text/javascript' src='assets/one_marker_gmaps.js'></script>


                <body>
                    <div id="container">

                        <article class="entry">


                            <div class="entry-content">

                                <?php
                                     // Get lat and long by address         
                                        $address = $add; // Google HQ
                                        $prepAddr = str_replace(' ','+',$address);
                                        $geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address=458+4th+st,+Atwater,+California,+95301&key=AIzaSyAgo83mZXhQCFzF2Y3pQYJUC1ivAXKwiX4');
                                        $output= json_decode($geocode);
                                        $latitude = $output->results[0]->geometry->location->lat;
                                        $longitude = $output->results[0]->geometry->location->lng;

                                ?>

                                <?php /* === THIS IS WHERE WE WILL ADD OUR MAP USING JS ==== */ ?>
                                    <div class="google-map-wrap" itemscope itemprop="hasMap" itemtype="http://schema.org/Map">
                                        <div id="google-map" class="google-map">
                                        </div><!-- #google-map -->
                                    </div>

                                <?php /* === MAP DATA === */ ?>
                                <?php
                                    $locations = array();

                                    /* Marker #1 */
                                    $locations[] = array(
                                        'google_map' => array(
                                            'lat' => $latitude,
                                            'lng' => $longitude,
                                        ),
                                        'location_address' => 'Puri Anjasmoro B1/22 Semarang',
                                        'location_name'    => 'Loc A',
                                );

                                ?>


                                <?php /* === PRINT THE JAVASCRIPT === */ ?>

                                <?php
                                    /* Set Default Map Area Using First Location */
                                    $map_area_lat = isset( $locations[0]['google_map']['lat'] ) ? $locations[0]['google_map']['lat'] : '';
                                    $map_area_lng = isset( $locations[0]['google_map']['lng'] ) ? $locations[0]['google_map']['lng'] : '';
                                ?>

                                <script>
                                    jQuery( document ).ready( function($) {

                                        /* Do not drag on mobile. */
                                        var is_touch_device = 'ontouchstart' in document.documentElement;

                                        var map = new GMaps({
                                            el: '#google-map',
                                            lat: '<?php echo $map_area_lat; ?>',
                                            lng: '<?php echo $map_area_lng; ?>',
                                            scrollwheel: true,
                                            zoom: 5,
                                            draggable: ! is_touch_device
                                        });

                                    /* Map Bound */
                                    var bounds = [];

                                    <?php /* For Each Location Create a Marker. */
                                        foreach( $locations as $location ){
                                            $name2 = $location['location_name'];
                                            $addr = $location['location_address'];
                                            $map_lat = $location['google_map']['lat'];
                                            $map_lng = $location['google_map']['lng'];
                                            ?>
                                            /* Set Bound Marker */
                                            var latlng = new google.maps.LatLng(<?php echo $map_lat; ?>, <?php echo $map_lng; ?>);
                                            bounds.push(latlng);
                                            /* Add Marker */
                                            map.addMarker({
                                                lat: <?php echo $map_lat; ?>,
                                                lng: <?php echo $map_lng; ?>,
                                                title: '<?php echo $name; ?>',
                                                infoWindow: {
                                                    content: '<?php echo $name; ?>'
                                                }
                                            });
                                    <?php } //end foreach locations ?>

                                        /* Fit All Marker to map */
                                        map.fitLatLngBounds(bounds);

                                        /* Make Map Responsive */
                                        var $window = $(window);
                                        function mapWidth() {
                                            var size = $('.google-map-wrap').width();
                                            $('.google-map').css({width: size + 'px', height: (size/2) + 'px'});
                                        }
                                        mapWidth();
                                        $(window).resize(mapWidth);

                                    });
                                </script>

2 个答案:

答案 0 :(得分:0)

当我测试时,看起来file_get_contents返回false。你试过卷曲吗?

$url = 'https://maps.google.com/maps/api/geocode/json?address=458+4th+st,+Atwater,+California,+95301&key=AIzaSyAgo83mZXhQCFzF2Y3pQYJUC1ivAXKwiX4';

$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$output= json_decode($data);

答案 1 :(得分:0)

此答案可能有用。.

您应该为Google地图位置选择器提供SSL。它不适用于http://ex.com,仅适用于https://ex.com