谷歌地图不是以HTML格式显示,而是在JS Fiddle上工作

时间:2016-05-18 19:43:12

标签: javascript jquery html google-maps jsfiddle

我有一些我在JSFiddle中一直在玩的代码,并设法让它完全按照我的意愿运行。 JSFiddle中的输出窗口提供了正确的输出,但每当我尝试让代码在网页中工作时,地图就不会显示。我尝试了几种方法,包括直接从Chrome中的视图框选项获取代码:

<!DOCTYPE html>
<html>

<head>

  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="robots" content="noindex, nofollow">
  <meta name="googlebot" content="noindex, nofollow">

  <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>

    <link rel="stylesheet" type="text/css" href="/css/result-light.css">

      <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true_or_false"></script>

  <style type="text/css">
     #map-canvas {
    width:800px;
    height:800px
}
  </style>

  <title></title>

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
var map;
var infowindow;
var searchData = null;
var georssLayer;

myLatLng = new google.maps.LatLng(53.494170, -1.146462);
var mapOptions = {
    zoom: 9,
    center: myLatLng
}

var kmlURL = 'http://stashbox.org/1491090/proposed_school.kml'


map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
infowindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);

georssLayer = new google.maps.KmlLayer(kmlURL,{preserveViewport:true});
georssLayer.setMap(map);

//used pipes to convert kml to json and loaded it
$.ajax("http://pipes.yahoo.com/pipes/pipe.run?_id=10a81555228e77349570df1cc6823ed2&_render=json&urlinput1=" + kmlURL).done(function (data) {
    console.log('here')
    searchData=data.value.items[0].Document.Placemark //structrue of pipe
})

function createMarker(place) {
    var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location
    });

    google.maps.event.addListener(marker, 'click', function () {
        service.getDetails(place, function(result, status) {
              if (status != google.maps.places.PlacesServiceStatus.OK) {
                alert(status);
                return;
              }
              infowindow.setContent(result.name);
              infowindow.open(map, marker);
            });
        });
}

function createMarkerKML(place){
    var loc=place.Point.coordinates.split(",")
    var marker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(loc[1],loc[0])
    });
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.setContent(place.name);
        infowindow.open(map, marker);
    })
}

function searchKML(request,callback){
    var ret=[]
    if(!searchData) return
    for(var i=0;i<searchData.length;i++){
        //insert distance search
        if( searchData[i].description.indexOf(request.keyword)!=-1 ||   //currently case sensitive
            searchData[i].name.indexOf(request.keyword)!=-1 ){
            ret.push(searchData[i])
        }
    }
    callback(ret)
}


function searchMap(str) {
    var request = {
        location: map.getCenter(),
        radius: '2500',
        keyword: str
    };
    service.radarSearch(request, callback); //normal search
    searchKML(request,function(results){
        for (var i = 0; i < results.length; i++) {
            createMarkerKML(results[i]);
        }
    })

}

function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
       console.log(results)
        for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
        }
    }
}

searchform.onsubmit = function (e) {
    e.preventDefault();
    searchMap(document.getElementById("search").value);

}

toggle.onclick = function () {
    if (toggle.value == "Hide KML Layer") {
        georssLayer.setMap(null)
        toggle.value = "Show KML Layer"
    } else {
        georssLayer.setMap(map)
        toggle.value = "Hide KML Layer"
    }

}
});//]]> 

</script>


</head>

<body>
  <form id="searchform">
    <input type="text" id="search" value="school" enabled/>
    <input type="submit" value="Search" />
</form>
<input type="button" value="Hide KML Layer" id="toggle" />
<div id="map-canvas"></div>

</body>

</html>

有关为什么我无法将其显示在HTML中的任何帮助/想法/建议?看了几个不同的旧问题,并尝试了一些建议,但仍然没有悲伤的喜悦。使用Windows 10&amp;最新的谷歌浏览器版本,如果它有帮助。

谢谢, SIE

1 个答案:

答案 0 :(得分:0)

可能是jQuery没有加载吗?尝试更改

// code.jquery.com/jquery-1.10.1.js to:

https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js