2016-03-25 45 views
0

這對於localhost非常有用,但無法在線工作。我到處尋找解決辦法。我已經嘗試了一個用於服務器和瀏覽器的Google密鑰。無法使用地理編碼在線工作,但在localhost上工作

當我把URL放到瀏覽器中時,我得到了經緯度和長度的正確響應。但是,一旦我上傳代碼,它不會拉動經緯度。我可以手動放置拉特和長期,它很好。任何幫助將是驚人的。

"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> 
+0

check var_dump(output);可能是您已經超出了此API的每日請求限額。你的服務器在共享主機上嗎?其他人可能已經用完了配額 – channasmcs

回答

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); 
相關問題