Google 地图 v3、JSON、对象和 Opera
我正在使用 http://maps.google.com/maps/api/js? Sensor=false 检索地址的纬度和经度。
从 google 检索数据时,我将纬度和经度存储到数组 myLat 和 myLng 中。
最终用户在文本区域中输入地址以及建筑物名称,这些建筑物名称也存储在他们自己的数组 schoolAddressArray 和 schoolNameArray 中。
我将结果放在一个名为 results 的 div 中——
document.getElementById("results")
然后我获取构造的字符串并使用 eval 将其转换为“类似 JSON”的对象。
PHP start
global $browser;
if(strpos($_SERVER["HTTP_USER_AGENT"], "Opera") !== FALSE)
{
$browser = "opera";
}
if(strpos($_SERVER["HTTP_USER_AGENT"], "MSIE") !== FALSE)
{
$browser = "msie";
}
if(strpos($_SERVER["HTTP_USER_AGENT"], "Firefox") !== FALSE)
{
$browser = "ff";
}
if(strpos($_SERVER["HTTP_USER_AGENT"], "Safari") !== FALSE)
{
$browser = "safari";
}
echo'
<script type="text/javascript">
<!--
var $whichBrowser = "'; echo $browser; echo'";
-->
</script>
';
PHP end
这是 JavaScript 开始的地方
var strlen = tempVar.length;
var myNewTempVar = tempVar.slice(0,strlen-2)+']';
document.getElementById("results").innerHTML = myNewTempVar;
if($whichBrowser == "msie")
{
//alert(document.getElementById("results").childNodes[0].nodeValue);
markers = window.eval(document.getElementById("results").childNodes[0].nodeValue);
}
else if($whichBrowser == "opera")
{
//markers = window.eval([getProperty(tempVar.slice(1,strlen-2))]);
//alert(markers);
//markers = [{ lat: 34.0878234, lng: -118.1543285, name: "1 school"}, { lat: 34.071239, lng: -118.1506133, name: "2 school"}, { lat: 34.1015362, lng: -118.1328816, name: "3 school"}, { lat: 34.098139499, lng: -118.1168382, name: "4 school"}, { lat: 34.0751855, lng: -118.1381771, name: "5 school"}, { lat: 34.0856624, lng: -118.1345840, name: "6 school"}, { lat: 34.0951319, lng: -118.1421722, name: "7 school"}, { lat: 34.0760016, lng: -118.1281349, name: "8 school"}, { lat: 34.0621694, lng: -118.1291052, name: "9 school"}, { lat: 34.0583261, lng: -118.1563075, name: "10 school"}, { lat: 34.046435, lng: -118.1524630, name: "11 school"}, { lat: 34.054128899, lng: -118.1280777, name: "12 school"}];
for(var myX=0;myX<myLat.length;myX++)
{
markers.push(["{ lat: "+myLat[myX]+", lng: "+myLng[myX]+", name: "+schoolNameArray[myX]+"}"]);
}
alert(markers);
}
else
{
markers = eval('('+myNewTempVar+')');
}
JavaScript End
如您所见,我使用 PHP
strpos($_SERVER["HTTP_USER_AGENT"]
来确定最终用户正在使用什么浏览器,然后回显一些 javascript 以设置变量 $whichBrowser。自 2000 年代初以来,这一直对我有用——顺便说一句,我不知道该怎么称呼它。 90后就是90后。这些是10几吗? 0?
无论如何,IE、FF 和 Safari 的代码工作得很好,但我已经尝试了 Opera 能想到的一切,但它只是不会将标记视为对象。
我已经从结果 div 中复制了构建的字符串,并对它进行了硬编码,以查看它是否在 Opera 中起作用,它完全没有错误。
我也尝试过:
for(index in myLat)
{
markers[index].lat = myLat[index];
markers[index].lng = myLng[index];
markers[index].name = schoolNameArray[index];
}
我收到了从无法将标记[索引]转换为对象到未捕获异常的错误:RangeError:超出最大递归深度
这是我正在使用的测试数据:
2821 W. Commonwealth Ave., Alhambra, CA
2001 S. Elm St., Alhambra, CA
110 W. McLean St., Alhambra, CA
100 S. Granada Ave., Alhambra, CA
1603 S. Marguerita Ave., Alhambra, CA
409 S. Atlantic Blvd., Alhambra, CA
301 N. Marengo Ave., Alhambra, CA
509 W. Norwood Pl., Alhambra, CA
120 S. Ynez Ave., Monterey Park, CA
400 Casuda Canyon Dr., Monterey Park, CA
1701 Brightwood St., Monterey Park, CA
650 Grandridge Ave., Monterey Park, CA
这是我的测试服务器的链接-- http://kronusdevelopments.com/mycarpaysme_ajax/test_street_view13.php
如您所见,没有错误在 IE、Safari 和 FF 中都有报道,但 Opera 是一种不同的游戏。
Safari 中对 google 返回的 svgs 出现一些警告,FF 中出现一些 css 警告,但错误数为 0。当 google 未返回地址时,IE 会抱怨,但在 Opera 中修复此问题后,我将为 google 输入某种 try and catch 代码。
我也尝试过将字符串发送到函数并返回评估结果,但到目前为止没有任何效果。
预先感谢,我希望我遵守了在这个论坛上发帖的所有规则。
I am using http://maps.google.com/maps/api/js?sensor=false to retrieve latitude and longitude for address.
While retrieving the data from google, I store the lats and lngs into arrays myLat and myLng.
The end user enters addresses into a textarea along with the names of the buildings which are also stored into their own arrarys schoolAddressArray and schoolNameArray.
I place the results in a div called results --
document.getElementById("results")
Then I take the constructed string and use eval to convert it into a "JSON like" object.
PHP start
global $browser;
if(strpos($_SERVER["HTTP_USER_AGENT"], "Opera") !== FALSE)
{
$browser = "opera";
}
if(strpos($_SERVER["HTTP_USER_AGENT"], "MSIE") !== FALSE)
{
$browser = "msie";
}
if(strpos($_SERVER["HTTP_USER_AGENT"], "Firefox") !== FALSE)
{
$browser = "ff";
}
if(strpos($_SERVER["HTTP_USER_AGENT"], "Safari") !== FALSE)
{
$browser = "safari";
}
echo'
<script type="text/javascript">
<!--
var $whichBrowser = "'; echo $browser; echo'";
-->
</script>
';
PHP end
This is where the JavaScript starts
var strlen = tempVar.length;
var myNewTempVar = tempVar.slice(0,strlen-2)+']';
document.getElementById("results").innerHTML = myNewTempVar;
if($whichBrowser == "msie")
{
//alert(document.getElementById("results").childNodes[0].nodeValue);
markers = window.eval(document.getElementById("results").childNodes[0].nodeValue);
}
else if($whichBrowser == "opera")
{
//markers = window.eval([getProperty(tempVar.slice(1,strlen-2))]);
//alert(markers);
//markers = [{ lat: 34.0878234, lng: -118.1543285, name: "1 school"}, { lat: 34.071239, lng: -118.1506133, name: "2 school"}, { lat: 34.1015362, lng: -118.1328816, name: "3 school"}, { lat: 34.098139499, lng: -118.1168382, name: "4 school"}, { lat: 34.0751855, lng: -118.1381771, name: "5 school"}, { lat: 34.0856624, lng: -118.1345840, name: "6 school"}, { lat: 34.0951319, lng: -118.1421722, name: "7 school"}, { lat: 34.0760016, lng: -118.1281349, name: "8 school"}, { lat: 34.0621694, lng: -118.1291052, name: "9 school"}, { lat: 34.0583261, lng: -118.1563075, name: "10 school"}, { lat: 34.046435, lng: -118.1524630, name: "11 school"}, { lat: 34.054128899, lng: -118.1280777, name: "12 school"}];
for(var myX=0;myX<myLat.length;myX++)
{
markers.push(["{ lat: "+myLat[myX]+", lng: "+myLng[myX]+", name: "+schoolNameArray[myX]+"}"]);
}
alert(markers);
}
else
{
markers = eval('('+myNewTempVar+')');
}
JavaScript End
As you can see, I use PHP's
strpos($_SERVER["HTTP_USER_AGENT"]
to figure out what browser is being used by the end user followed by echoing some javascript to set the variable $whichBrowser. This has worked for me since early 2000s -- BTW, I don't know what to call that. The 90s are the 90s. Are these the 10s? The 0s?
Anyways, the code for IE, FF and Safari work fine, but I have tried everything that I could think of with Opera and it just will not see markers as an object.
I have copied the constructed string from the results div and hard coded it to see if it worked at all in opera, which it does with no errors at all.
I have also tried:
for(index in myLat)
{
markers[index].lat = myLat[index];
markers[index].lng = myLng[index];
markers[index].name = schoolNameArray[index];
}
I have received errors ranging from cannot convert markers[index] into an object to Uncaught exception: RangeError: Maximum recursion depth exceeded
Here is the test data that I am using:
2821 W. Commonwealth Ave., Alhambra, CA
2001 S. Elm St., Alhambra, CA
110 W. McLean St., Alhambra, CA
100 S. Granada Ave., Alhambra, CA
1603 S. Marguerita Ave., Alhambra, CA
409 S. Atlantic Blvd., Alhambra, CA
301 N. Marengo Ave., Alhambra, CA
509 W. Norwood Pl., Alhambra, CA
120 S. Ynez Ave., Monterey Park, CA
400 Casuda Canyon Dr., Monterey Park, CA
1701 Brightwood St., Monterey Park, CA
650 Grandridge Ave., Monterey Park, CA
Here's the link to my test server --
http://kronusproductions.com/mycarpaysme_ajax/test_street_view13.php
As you can see there are no errors reported in IE, Safari and FF, but Opera is a different ball game.
Some warnings in Safari for the svgs returned by google and some css warnings in FF, but 0 errors. IE complains when an address is not returned by google, but I will enter some kind of try and catch code for google after fixing this issue in Opera.
I have tried also sending the string to a function and return the evaluation, but nothing has worked so far.
Thanks in advance and I hope I have followed all the rules to posting to this forum.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何现代浏览器都会有自己的 JSON 解析器:
使用它而不是
eval
也不需要浏览器检测。好吧,除非你真的想获得那些 IE6 用户。
Any modern browser will have its own JSON parser:
Use that and NOT
eval
No need for browser detection, either. Well, unless you really want to get those IE6 users.
这
应该是
this
should be