'未捕获InvalidValueError'(:setCenter:不是LatLng或LatLngLiteral对象) [英] 'Uncaught InvalidValueError' (: setCenter: not a LatLng or LatLngLiteral object)

查看:254
本文介绍了'未捕获InvalidValueError'(:setCenter:不是LatLng或LatLngLiteral对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我计算了如何放置各种标记,但控制台会给我一个错误:

未捕获InvalidValueError:setCenter:不是LatLng或LatLngLiteral对象。



我认为这是因为在循环中创建的var不完全是需要的对象,而是一个字符串,尽管这使我困惑,但我不知道我是否正确。如果有人能让我走上正确的道路,我将非常感激。


预先感谢。

这是我的代码:
(顺便说一下,我循环的数组是一个数组,其中包含一些数组。它遵循这个模式,总共有6个地方):

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ array $(
array(
'Location'=> array(
'Title'=>'Place',
'Description'=>'Some Place',
'Image'=> ;'pin1.png',
'Latitude'=> '20 .681775',
'Longitude'=>' - 103.351479'

),...

(它会继续...)。

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

< script type =text / javascript
src =https://maps.googleapis。 ?COM /地图/ API / JS AIzaSyBVfO8LckdOHAot1a8rZW0bmJIoWO2A3os = API_KEY&安培;传感器=真>
< / script>


< script type =text / javascript>
函数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);

//要将标记添加到地图,请使用'map'属性
<?php $ i = 0; ?>
<?php foreach($ locations为$ 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(地图);

<?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分配部分的屏幕截图: http://cl.ly/image/1d3e2J360t3y

解决方案

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

显然,解决方案是传入一个数字,或者将数据包装在 parseInt() parseFloat(),以便API获取数字或浮点数。 Google应该正在清理其数据,但事实并非如此。

  var custom_icon = new google.maps.MarkerImage(
icon_path,
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)

);


I'm starting to know PHP and JavaScript, mixing them in HTML and using Google Maps API (version 3).

I figured how to 'put' various markers but the console will throw me an error:

" Uncaught InvalidValueError: setCenter: not a LatLng or LatLngLiteral object ".

I think it's because the var created in the loop is not exactly the object needed but instead a string, though this confuses me, I don't know if I'm right. I'd appreciate it a lot if anyone could set me in the right path.

Thanks in advance.

This is my code: (By the way, the array I'm looping is an array with an array with an array, here's some of it. It follows the pattern, there are in total 6 'places'):

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

(It continues…).

<!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>

And here's a screenshot of that var assigning part: " http://cl.ly/image/1d3e2J360t3y "

解决方案

It is also worth noting that Google Maps v3 doesn't appear to sanitize the data values for setting markers. This seems to have changed in the last few weeks, because previously I was passing in icon height as a string for google's MarkerImage function (it is coming from JSON). But that suddenly broke because it wasn't a "number".

Obviously the solution is to pass in a number, or wrap your data in parseInt() or parseFloat() so that the API gets a number or float. Google "should" be sanitizing it's data but it isn't.

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)
    )
);

这篇关于'未捕获InvalidValueError'(:setCenter:不是LatLng或LatLngLiteral对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆