doupinge9055 2013-01-31 22:26 采纳率: 100%
浏览 83

未捕获的SyntaxError:意外的令牌<; 未捕获的ReferenceError:未定义initialize

I know this has been asked before, but I can't seem to find the error in my code:

Here it is below (it's javascript used for Google Maps with a php call inside to dynamically make an array of latitudes and longitudes user for markers):

<script type="text/javascript">

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;


function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();

    var mapOptions = {
    zoom: 5,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
                              mapOptions);
    directionsDisplay.setMap(map);

    var markers = [

    <?php

    //orgnize fans by city
    $query = "SELECT city, state, COUNT(*) fans FROM users GROUP BY city ORDER BY fans DESC";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){

    //pulls the city, state code from the database and stores it as a string in $address
    $address = urlencode('"' . $row['city'] . ", " . $row['state'] . '"');
    $googleApi = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false';     

    $json = file_get_contents(sprintf($googleApi, $address));   
    $resultObject = json_decode($json);

    $location = $resultObject->results[0]->geometry->location;

    $lat = $location->lat;
    $lng = $location->lng;

        echo "{ lat: ".$lat.", lng: ".$lng.", name: ".'"'.$row['city'].", ".$row['state'].'"'."},";
    }

    ?>

    ];

    // Create the markers ad infowindows.
    for (index in markers) addMarker(markers[index]);
    function addMarker(data) {
        // Create the marker
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.lng),
            map: map,
            title: data.name
        });

        // Create the infowindow with two DIV placeholders
        // One for a text string, the other for the StreetView panorama.
        var content = document.createElement("DIV");
        var title = document.createElement("DIV");
        title.innerHTML = data.name;
        content.appendChild(title);
        var streetview = document.createElement("DIV");
        streetview.style.width = "200px";
        streetview.style.height = "200px";
        content.appendChild(streetview);
        var infowindow = new google.maps.InfoWindow({
            content: content
        });

        // Open the infowindow on marker click
        google.maps.event.addListener(marker, "click", function() {
            infowindow.open(map, marker);
        });

        // Handle the DOM ready event to create the StreetView panorama
        // as it can only be created once the DIV inside the infowindow is loaded in the DOM.
        google.maps.event.addListenerOnce(infowindow, "domready", function() {
            var panorama = new google.maps.StreetViewPanorama(streetview, {
            navigationControl: false,
            enableCloseButton: false,
            addressControl: false,
            linksControl: false,
            visible: true,
            position: marker.getPosition()
            });
       });

    }

    // Try HTML5 geolocation
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var pos = new google.maps.LatLng(position.coords.latitude,
            position.coords.longitude);

            var infowindow = new google.maps.InfoWindow({
            map: map,
            position: pos,
            content: 'Your Current City'
        });

            map.setCenter(pos);
        }, function() {
        handleNoGeolocation(true);
        });

    } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
    }

}

function handleNoGeolocation(errorFlag) {
    if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
    } else {
        var content = 'Error: Your browser doesn\'t support geolocation.';
    }

    var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
    };

    var infowindow = new google.maps.InfoWindow(options);
    map.setCenter(options.position);
}


function calcRoute() {
    var start = document.getElementById('start').value;
    var end = document.getElementById('end').value;
    var waypts = [];
    var checkboxArray = document.getElementById('waypoints');
    for (var i = 0; i < checkboxArray.length; i++) {
        if (checkboxArray.options[i].selected == true) {
            waypts.push({
                        location:checkboxArray[i].value,
                        stopover:true});
        }
    }

    var request = {
    origin: start,
    destination: end,
    waypoints: waypts,
    optimizeWaypoints: true,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
                            if (status == google.maps.DirectionsStatus.OK) {
                            directionsDisplay.setDirections(response);
                            var route = response.routes[0];
                            var summaryPanel = document.getElementById('directions_panel');
                            summaryPanel.innerHTML = '';
                            // For each route, display summary information.
                            for (var i = 0; i < route.legs.length; i++) {
                            var routeSegment = i + 1;
                            summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
                            summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
                            summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
                            summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
                            }
                            }
                            });
}







google.maps.event.addDomListener(window, 'load', initialize);


</script>

<body onload="initialize()">

<center><div id="map_canvas" style="width: 1100px; height: 450px;">map div</div></center>

etc...

Here is the javascript console output for the php portion:

    var markers = [

    <br />
Uncaught SyntaxError: Unexpected token <
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22San+Francisco%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "San Francisco, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22New+York%2C+NY%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "New York, NY"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Los+Angeles%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Los Angeles, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Oakland%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Oakland, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22San+Diego%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "San Diego, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Lafayette%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Lafayette, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Chester+Springs%2C+PA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Chester Springs, PA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Berkeley%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Berkeley, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Gilbert%2C+AZ%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Gilbert, AZ"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Martinez%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Martinez, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Queens%2C+NY%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Queens, NY"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Manhattan%2C+NY%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Manhattan, NY"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Brier%2C+WA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Brier, WA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Sacramento%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Sacramento, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Pacific+Beach%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Pacific Beach, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22New+London%2C+CT%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "New London, CT"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Santa+Cruz%2C+CA%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Santa Cruz, CA"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Jackson%2C+MS%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Jackson, MS"},<br />
<b>Warning</b>:  file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
<br />
<b>Warning</b>:  file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=%22Honolulu%2C+HI%22&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: no suitable wrapper could be found in <b>/nfs/c09/h04/mnt/139243/domains/crowdsets.com/html/maptest.php</b> on line <b>202</b><br />
{ lat: , lng: , name: "Honolulu, HI"},    
    ];
  • 写回答

1条回答 默认 最新

  • dongmimeng5500 2013-01-31 22:38
    关注

    Error message says that file_get_contents can't access http:// protocol (disabled in php.ini). You can enable it by changing allow_url_fopen=1 in php.ini, read more at http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen . If that's not possible (you don't have server config access), you could use one of those instead of file_get_contents:

    1. php native socket http://php.net/manual/en/book.sockets.php
    2. php curl extension (has to be enabled/installed to server): http://php.net/manual/en/book.curl.php
    3. Zend_Http_Client wrapper for other adapters http://framework.zend.com/manual/1.12/en/zend.http.client.html

    It's much easier to catch and handle errors with one of those suggested extensions/libraries.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据