问题描述:

I am searching for an eval() alternative..

I generate a usal google map, API v3.:

 // define options

var mapOptions = {

zoom: 18,

center: new google.maps.LatLng(-33.890542, 151.274856),

mapTypeId: eval(maptype["map"]),

panControl: true,

zoomControl: true,

mapTypeControl: true,

scaleControl: true

};

// create map

map = new google.maps.Map(document.getElementById('map_test),mapOptions);

This works as it should, but I want to avoid eval().

maptype is defined via php, using JSON:

echo 'maptype = '.json_encode($options).';';

Is there a alternative for eval?

Update:

the full sourcecode:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Unbenanntes Dokument</title>

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>

<script src="https://maps.googleapis.com/maps/api/js?v=3.7&sensor=false&language=de"></script>

</head>

<body>

<script>

maptype = {"map":"google.maps.MapTypeId.SATELLITE"};</script>

<script>

function Map()

{

var mapOptions = {zoom: 18, center: new google.maps.LatLng(-33.890542, 151.274856), mapTypeId: eval(maptype["map"])};

// create map

map = new google.maps.Map(document.getElementById('map'),mapOptions);

}

</script>

<div id="map" style="width:1200px; height:800px"></div>

<script> jQuery(document).ready(function ($) {Map();});</script>

</body>

</html>

Result: no error, no map, just a grey background.

网友答案:

You should be able to just remove the eval() call around it and it should work, assuming ngggOptions is an object and has a key for map.

相关阅读:
Top