doulongti5932 2014-10-05 16:11
浏览 61

Google Maps API为完整位置和部分位置提供错误

I came across a class for integrating google maps into my website at www.TrackTheSpraying.com. Now to explain the concept here basically the end user submits a location (Full Address or City,State) than it pins the location on the map.

After a few submissions i get an error saying no address found for location.

can someone please help me and look at the code here and tell me what causes this....

I have been stumped for quite a while now.

Thank you !

<?php

/**
 * Allowed Controls:
 * GLargeMapControl - a large pan/zoom control used on Google Maps. Appears in the top left corner of the map.
 * GSmallMapControl - a smaller pan/zoom control used on Google Maps. Appears in the top left corner of the map.
 * GSmallZoomControl - a small zoom control (no panning controls) used in the small map blowup windows used to display driving directions steps on Google Maps.
 * GScaleControl - a map scale
 * GMapTypeControl - buttons that let the user toggle between map types (such as Map and Satellite)
 * GOverviewMapControl - a collapsible overview map in the corner of the screen
 */

  define(GoogleMapsKey, ''); 

  define( GLargeMapControl      , 'GLargeMapControl()');
  define( GSmallMapControl      ,    'GSmallMapControl()');
  define( GSmallZoomControl     , 'GSmallZoomControl()');
  define( GScaleControl         , 'GSCALEControl()');
  define( GMapTypeControl       , 'GMapTypeControl()');
  define( GOverviewMapControl , 'GOverviewMapControl()');

/**
 * API-Class for accessing Google Maps 
 */
class NXGoogleMapsAPI {

  // The Google Maps API Key
  var $apiKey;

  // Width and Height of the Control
  var $width;
  var $height;

  // GoogleMaps output div id
  var $divId;

  // ZoomFactor
  var $zoomFactor;

  // Map Center Coords
  var $centerX;
  var $centerY;

  // DragMarker
  var $dragX;
  var $dragY;

  // Address Array
  var $addresses;

  // GeoPoint Array
  var $geopoints;

  // Arrays with the controls that will be displayed
  var $controls;


  /**
   * Constructor
   *
   * @param string $apiKey The Google Maps API-Key for your domain.
   */
  function NXGoogleMapsAPI($apiKey="") {
    $this->apiKey = $apiKey;
    if ($this->apiKey == "") 
      $this->apiKey = GoogleMapsKey;
    $this->_initialize();
  }


  /**
   * Add an address-marker to the map. The address is resolved by the webbrowser.
   * with the Google Geocoder.
   *
   * @param string address which should be add. test with google maps
   * @param string HTML-Code which will be displayed when the user clicks the address
   * @param boolean Set the Center to this point(true) or not (false)
   */
  function addAddress($address, $htmlinfo, $setCenter=true) {
    $ar = array(addSlashes($address), addSlashes($htmlinfo), $setCenter);
    array_push($this->addresses, $ar);  
  }

  /**
   * Add a dragable marker to the map. Only one Drag-Marker is allowed!
   *
   * @param integer $longitude Longitude of the point
   * @param integer $latitude  Lattitude of the point
   */
  function addDragMarker($longitude, $latitude) {
    $this->dragX = $longitude;
    $this->dragY = $latitude;   
  }

  /**
   * Add a geopoint to the map. 
   *
   * @param integer Longitude of the point
   * @param integer Latitude of the point
   * @param string HTML-Code which will be displayed when the user clicks the address
   * @param boolean Set the Center to this point(true) or not (false)
   */  
  function addGeoPoint($longitude, $latitude, $htmlinfo, $setCenter) {
    $ar = array($longitude, $latitude, addSlashes($htmlinfo), $setCenter);
    array_push($this->geopoints, $ar);  
  }

  /**
   * Adds a control to the map
   *
   * @param control Control-Type. Allowed are the constants 
   * GLargeMapControl - a large pan/zoom control used on Google Maps. Appears in the top left corner of the map.
   * GSmallMapControl - a smaller pan/zoom control used on Google Maps. Appears in the top left corner of the map.
   * GSmallZoomControl - a small zoom control (no panning controls) used in the small map blowup windows used to display driving directions steps on Google Maps.
   * GScaleControl - a map scale
   * GMapTypeControl - buttons that let the user toggle between map types (such as Map and Satellite)
   * GOverviewMapControl - a collapsible overview map in the corner of the screen
   *      
   */      
  function addControl($control) {
    array_push($this->controls, $control);
  }

  /**
   * Set the ZoomFactor
   * The ZoomFactor is a value between 0 and 17
   *
   * @param integer $zoomFactor Value of the Zoom-Factor
   */
  function setZoomFactor($zoomFactor) {
     if ($zoomFactor > -1 && $zoomFactor < 18) {
       $this->zoomFactor = $zoomFactor;
     }
  }

  /**
   * Set the width of the map
   *
   * @param integer $width The Height in pixels
   */
  function setWidth($width) {
    $this->width = $width;
  }

  /**
   * Set the height of the map
   *
   * @param integer $height The Height in pixels
   */
  function setHeight($height) {
      $this->height = $height;
  }

  /**
   * Center the map to the coordinates
   *
   * @param integer $x Longitude
   * @param integer $y Latitude
   */
  function setCenter($x, $y) {
    $this->centerX = $x;
    $this->centerY = $y;
  }


  /**
   * Returns the HTML-Code, which must be placed within the <HEAD>-Tags of your page.
   *
   * @returns string The Code for the <Head>-Tag
   */
  function getHeadCode() {
    $out = '
 <style type="text/css">
     v\:* {
       behavior:url(#default#VML);
     }
    </style>
    <script src="http://maps.google.com/maps?file=api&v=2&key='.$this->apiKey.'"  type="text/javascript"></script>';
   $out.= $this->_getGMapInitCode();
   return $out;
  }

  /**
   * Get the BodyCode and draw the map.
   *
   * @returns string Returns the code which is to be placed wight the <body>-tags.
   */
  function getBodyCode() {
    $out = '<div id="'.$this->divId.'" style="width:'.$this->width.'px;height:'.$this->height.'px;"></div>';    
    return $out;
  }

  /**
   * Get the code, which must be passed to the <body>-attribute onLoad.
   *
   * @returns string The onload Code
   */
  function getOnLoadCode() {
    $out = "initNXGMap(document.getElementById('$this->divId'));";
    return $out;
  }


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// Internal functions /////////////////////////////////////////////////////////////////////////////////  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////  

  /** 
   * Compiles the Javascript to initialize the map.
   * Is automatically called, so do not call yourself.
   */
  function _getGMapInitCode() {
    $out = '
<script type="text/javascript">
//<![CDATA[
var map = null;
var geocoder = null;
var center = null;
var updateX = null;
var updateY = null;
var marker = null;';

             // Add Geopoints Array
             $out.="
";
      if ( count($this->geopoints) > 0 ) {          
        $out.= 'var geopoints = new Array(';
        for ($i=0; $i < count($this->geopoints); $i++) {
            $out.= ' new Array('.$this->geopoints[$i][0].','.$this->geopoints[$i][1].' ,"'.$this->geopoints[$i][2].'", ';
            // move to this address?
            if ($this->geopoints[$i][3]) {
                $out.='true';
            } else {
                $out.='false';
            }
            $out.=')';
            if ($i < (count($this->geopoints)-1)) $out.=', ';
        }
        $out.=");
";
      } else {
          $out.="var geopoints = new Array();
";
      }

             // Add Addresses Array      
      $out.="
";
      if ( count($this->addresses) > 0 ) {          
        $out.= 'var addresses = new Array(';
        for ($i=0; $i < count($this->addresses); $i++) {
            $out.= ' new Array("'.$this->addresses[$i][0].'", "'.$this->addresses[$i][1].'", ';
            // move to this address?
            if ($this->addresses[$i][2]) {
                $out.='true';
            } else {
                $out.='false';
            }
            $out.=')';
            if ($i < (count($this->addresses)-1)) $out.=', ';
        }
        $out.=");
";
      } else {
          $out.="var addresses = new Array();
";
      }

        // Draw standard js-functions and initialization code.
        $out.='
function showAddresses() {
  for (i=0; i < addresses.length; i++) {
        showAddress(addresses[i][0], addresses[i][1], addresses[i][2]);
  } 
}

function showAddress(address, htmlInfo, moveToPoint) {
 if (geocoder) {
   geocoder.getLatLng(
     address,
     function(point) {
       if (!point) {
         alert("Location not found:" + address);
       } else {              
         if (moveToPoint) {
           map.setCenter(point, '.$this->zoomFactor.');
         }
         var marker = new GMarker(point);
         map.addOverlay(marker);
         if (htmlInfo != "") {
           GEvent.addListener(marker, "click", function() {
              marker.openInfoWindowHtml(htmlInfo);
           });              
         }
       }
     }
   );
  }
}

function showGeopoints() {
  for (i=0; i < geopoints.length; i++) {
        showGeopoint(geopoints[i][0], geopoints[i][1], geopoints[i][2], geopoints[i][3]);
  } 
}

function showGeopoint(longitude, latitude, htmlInfo, moveToPoint) {
  if (moveToPoint) {
    map.setCenter(new GLatLng(longitude, latitude), '.$this->zoomFactor.');
  }
  var marker = new GMarker(new GLatLng(longitude, latitude));
  map.addOverlay(marker);
  if (htmlInfo != "") {
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(htmlInfo);
    });              
     }   
}

function moveToGeopoint(index) {
    map.panTo(new GLatLng(geopoints[index][0], geopoints[index][1]));
}

function moveToAddress(index) {
  moveToAddressEx(addresses[index][0]); 
}

function moveToAddressEx(addressString) {
  if (geocoder) { 
   geocoder.getLatLng(
     addressString,
     function(point) {       
       if (!point) {
         alert("Location not found:" + addressString);
       } else {                                    
          center = point;
          map.panTo(point);           
       }
     });    
  }
}

function moveToAddressDMarker(addressString) {
  if (geocoder) { 
   geocoder.getLatLng(
     addressString,
     function(point) {       
       if (!point) {
         alert("Location not found:" + addressString);
       } else {                                    
          center = point;
          setZoomFactor(14);
          map.panTo(point);  
          addDragableMarker();         
       }
     });    
  }
}

function setZoomFactor(factor) {
      map.setZoom(factor);
}

function addDragableMarker() {
  if (!marker) {
    marker = new GMarker(center, {draggable: true});
    map.addOverlay(marker);

    GEvent.addListener(marker, "dragend", function() {      
      var tpoint =  marker.getPoint();      
      document.getElementById(updateX).value = tpoint.lat();
      document.getElementById(updateY).value = tpoint.lng();              
  });

  } else {
    marker.setPoint(center);     
  }

  var tpoint =  marker.getPoint();      
  document.getElementById(updateX).value = tpoint.lat();
  document.getElementById(updateY).value = tpoint.lng();              
}

function initNXGMap(mapElement) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(mapElement);        
        geocoder = new GClientGeocoder();';

      // Add controls to the map

      if (count($this->controls) > 0) {
        for ($i=0; $i<count($this->controls); $i++) {
          $out.=" map.addControl(new ".$this->controls[$i].");
";
        }
      }

      // Center the map
      if (($this->centerX != -1000) && ($this->centerY != -1000)) {         
        $out.= '    map.setCenter(new GLatLng('.$this->centerX.', '.$this->centerY.'), '.$this->zoomFactor.');'."
";       
      } else {
        $out.= '    map.setCenter(new GLatLng(0,0),1);'."
";       
      }

      $out.='updateX="coordX"; updateY="coordY";';

      // Draw Dragmarker
      if (($this->dragX != 1000) && ($this->dragY != -1000)) {
        $out.='
          center = new GLatLng('.$this->dragY.','.$this->dragX.');
          map.setCenter(center, '.$this->zoomFactor.');
          marker = new GMarker(center, {draggable: true});
                map.addOverlay(marker);

                GEvent.addListener(marker, "dragend", function() {      
                var tpoint =  marker.getPoint();      
                document.getElementById(updateX).value = tpoint.lat();
                document.getElementById(updateY).value = tpoint.lng();              
                });
        ';
      }

      // Add AddressPoints
            $out.="    showAddresses();
";  
            // Add GeoPoints
            $out.="    showGeopoints();
";

     $out.="
";     
     $out.='    }
        }
     //]]>
     </script>';
    return $out;
  }

  /**
   * Initializes the standard values of the class. 
   * Is automatically called by the constructor.
   */
  function _initialize() {
    $this->width            = 800;
    $this->height       = 600;
    $this->divId        = 'map';
    $this->zoomFactor = 14;
    $this->centerX    = -1000;
    $this->centerY    = -1000;
    $this->dragX            = -1000;
    $this->dragY            = -1000;
    $this->addresses  = array();
    $this->geopoints  = array();
    $this->controls   = array();
  }

}

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划