'Uncaught InvalidValueError'(:setCenter:不是LatLng或LatLngLiteral对象)

时间:2013-11-15 07:00:24

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

我开始了解PHP和JavaScript,将它们混合在HTML中并使用Google Maps API(版本3)。

我想如何'放'各种标记,但控制台会给我一个错误:

  

“Uncaught InvalidValueError:setCenter:不是LatLng或LatLngLiteral对象”。

我认为这是因为在循环中创建的var并不完全是所需的对象,而是一个字符串,虽然这让我感到困惑,但我不知道我是不是正确。

这是我的代码: (顺便说一句,我循环的数组是一个带有数组的数组的数组,这里有一些。它遵循模式,共有6个'位置'):

$locations = array(
        array(
            'Location' => array(
                'Title'=>'Place',
                'Description'=>'Some Place',
                'Image'=>'pin1.png',
                'Latitude'=>'20.681775',
                'Longitude'=>'-103.351479'
            )
        ),…

(它继续......)。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map-canvas { height: 100% }
    </style>    

        <script type="text/javascript"
          src="https://maps.googleapis.com/maps/api/js?AIzaSyBVfO8LckdOHAot1a8rZW0bmJIoWO2A3os=API_KEY&sensor=true">
        </script>


      <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: (20.68177501, -103.3514794),
          zoom: 15,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);

        // To add the marker to the map, use the 'map' property
        <?php $i = 0; ?>
        <?php foreach($locations as $key => $value): ?>
                <?php foreach($value as $key => $value): ?>
                    <?php foreach($value as $key => $value): ?>
                        <?php if($key == "Latitude"): ?>
                            <?php $myLatLng = "$value, "; endif;?>
                        <?php if($key == "Longitude"): ?>
                            <?php $myLatLng .="$value"; ?>

        var myLatlng<?php echo $i; ?> = new google.maps.LatLng(<?php echo $myLatLng; ?>);
        var marker<?php echo $i; ?> = new google.maps.Marker({
        position: (myLatlng<?php echo $i; ?>),
        title:"Hello World!"
        });
        marker<?php echo $i; ?>.setMap(map); 

                            <?php $i++; ?>
                        <?php endif; ?>
                    <?php endforeach; ?>
                <?php endforeach; ?>
        <?php endforeach; ?>
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>


  </head>
  <body>
    <div id="map-canvas"/>
  </body>
</html>

这是var分配部分的屏幕截图:https://dzwonsemrish7.cloudfront.net/items/3J403M213b3J1Y231j0g/Screen%20Shot%202013-11-15%20at%2001.03.57.png?v=c4809ed2

3 个答案:

答案 0 :(得分:16)

您正在以错误的方式初始化mapOptions

按以下方式初始化..

var mapOptions = {
          center: new google.maps.LatLng(20.68177501, -103.3514794),
          zoom: 15,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

而不是......

var mapOptions = {
          center: (20.68177501, -103.3514794), //this is not correct
          zoom: 15,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

以下是更正后的代码...

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map-canvas { height: 100% }
    </style>    

        <script type="text/javascript"
          src="https://maps.googleapis.com/maps/api/js?AIzaSyBVfO8LckdOHAot1a8rZW0bmJIoWO2A3os=API_KEY&sensor=true">
        </script>


      <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(20.68177501, -103.3514794),
          zoom: 15,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);

        // To add the marker to the map, use the 'map' property
        <?php $i = 0; ?>
        <?php foreach($locations as $key => $value): ?>
                <?php foreach($value as $key => $value): ?>
                    <?php foreach($value as $key => $value): ?>
                        <?php if($key == "Latitude"): ?>
                            <?php $myLatLng = "$value, "; endif;?>
                        <?php if($key == "Longitude"): ?>
                            <?php $myLatLng .="$value"; ?>

        var myLatlng<?php echo $i; ?> = new google.maps.LatLng(<?php echo $myLatLng; ?>);
        var marker<?php echo $i; ?> = new google.maps.Marker({
        position: (myLatlng<?php echo $i; ?>),
        title:"Hello World!"
        });
        marker<?php echo $i; ?>.setMap(map); 

                            <?php $i++; ?>
                        <?php endif; ?>
                    <?php endforeach; ?>
                <?php endforeach; ?>
        <?php endforeach; ?>
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>


  </head>
  <body>
    <div id="map-canvas"/>
  </body>
</html>

答案 1 :(得分:10)

值得注意的是,Google Maps v3似乎无法清理设置标记的数据值。这似乎在过去几周发生了变化,因为之前我将图标高度作为谷歌MarkerImage函数的字符串传递(它来自JSON)。但那突然爆发,因为它不是一个数字&#34;。

显然,解决方案是传入一个数字,或者将数据包装在parseInt()parseFloat()中,以便API获取数字或浮点数。 Google&#34;应该&#34;正在消毒它的数据,但它不是。

var custom_icon = new google.maps.MarkerImage(
    icon_path,
    new google.maps.Size(
        parseInt(icon_options.width),
        parseInt(icon_options.height)
    ),
    new google.maps.Point(
        parseFloat(icon_options.origin_x),
        parseFloat(icon_options.origin_y)
    ),
    new google.maps.Point(
        parseFloat(icon_options.anchor_x),
        parseFloat(icon_options.anchor_y)
    )
);

答案 2 :(得分:-1)

尝试删除该行

<script type="application/javascript" src="google_maps/google_maps.js"></script>

并保持

<script type="application/javascript" src="http://maps.googleapis.com/maps/api/js?key=your-api-key"></script>