谷歌地图和空白错误

Google maps and white space errors

本文关键字:错误 空白 谷歌地图      更新时间:2023-09-26

我构建了一个应用程序,让我的客户输入标题,纬度,长度和内容。一切正常,直到他们在代码中包含一些空格,然后谷歌地图只是拒绝工作。防止这种情况的最佳方法是什么?我当前的代码如下:

<script type="text/javascript">
//Google map
function initialize() {
  var myOptions = {
    scrollwheel: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  // Create map on #map_canvas
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  // Define boundarys
  var markerBounds = new google.maps.LatLngBounds();
  var countries = [
    {title : 'M11 Moscow to St. Petersburg', lat : 55.938818, lon : 37.429733, marker : 'http://roadfree.org/images/map_markers/icon_map-marker.png', content : '<h4>M11 Moscow to St. Petersburg</h4><p>The M11 is a 650 km highway project aimed at connecting Moscow to Saint Petersburg. Despite several options proposed to build it, it was decided in 2009 that the road would run through Khimki Forest (between the 18 and 58 km marks), a centuries-old oak trees forest and host of many species of wildlife.</p><p>This decision provoked number of protests. On December 24 2011, nearly 100.000 persons rallied in Moscow, the largest demonstration in Russia since the fall of the Soviet Union.</p><p>The European Bank for Reconstruction and Development and the European Investment Bank, initially part of project, decided in 2011 to withdrawn their support.</p><p>The part of the road going through the forest is funded by private consortium including French giant construction group Vinci. Environmentalists and local residents denounce a case of corruption aimed at enriching property developers eager to increase housing facilities at the outskirts of Moscow.</p><p>The case of the M11 is also notorious because of the many reports of police brutality, intimidations, beatings and death of protesters and journalists.</p><p>The highway is due to open in 2014</p>'},
    {title : 'Title', lat : 1.576339, lon : 100.969849, marker : 'http://roadfree.org/images/map_markers/icon_map-marker_blue.png', content : '<h4>Title</h4><p>On the island of Sumatra in Indonesia, one of the world's richest ecosystem and host of the critically endangered sumatran elephant, rhinoceros, tiger and orangutan, road-building through intact forests and protected areas is a recurrent theme.</p><p>Kerinci Seblat,</p><p>The 1.33m ha Kerinci Seblat National Park (KSNP) in West Sumatra, is strictly protected under Indonesian law and an ASEAN Heritage Site and a UNESCO World Heritage Site.</p><p>Despite being illegal, plans to open four roads (up to 12 meters wide) through the park have been regularly supported by Aceh's consecutive governors for the last few years. For the local environmental NGOs denouncing the risks of fragmenting intact forest, destroying vulnerable ecosystems, and dividing breeding grounds and movement corridors, these plans are an aberration.</p><p>In June 2011, the Association for Tropical Biology and Conservation (ATBC) has drafted a resolution urging the Indonesian government to cancel plans to build roads through KSNP, the country's oldest national park.</p><p>Aceh,</p><p>The Ladia-Galaska road network planned consists in building 450 kilometres of main road plus more than 1200 kilometres of minor roads. It would turn 2.5m ha of rainforest into a patchwork of forest islands (see GIS model picture). This project has been discussed for a decade. Under the influence of various NGOs and external political intervention from the European Union, the project has so far been kept at bay.</p><p>In both cases, the following patterns have been identified:</p><p>Constant spatial revision plans to convert protected forests in production forests.</p><p>- Existence of roads often legitimises these spatial revision plans. Once a road is built, the forest is opened to logging, mining and others industrial activities.</p><p>- Logic is missing: some roads do not connect to human population centres of sufficiently large size or will cut through watershed forest in areas already prone to severe flooding.</p><p>Viable alternatives to constructing new routes do exist. They would be better for the biodiversity and better for the local populations.</p>'},
    {title : 'Title of third movement', lat : 3, lon : 3, marker : 'http://roadfree.org/images/map_markers/icon_map-marker_green.png', content : '<h4>Title of third movement</h4><p>Donec sed odio dui. Fusce dapibus, tellus ac cursus commodo, tortor mauris…</p>'},
    {title : 'Title of fouth movement', lat : 24, lon : 52, marker : 'http://roadfree.org/images/map_markers/icon_map-marker_red.png', content : '<h4>Title of fouth movement</h4><p>Donec sed odio dui. Fusce dapibus, tellus ac cursus commodo, tortor mauris…</p>'},
    {title : 'Title of fifth movement', lat : -24, lon : -24, marker : 'http://roadfree.org/images/map_markers/icon_map-marker.png', content : '<h4>Title of fifth movement</h4><p>Donec sed odio dui. Fusce dapibus, tellus ac cursus commodo, tortor mauris…</p>'},
  ];
  // Create markers
  for (var i = 0; i < countries.length; i++) {
        var c = countries[i];
        var latlng = new google.maps.LatLng(c.lat, c.lon);
        c.marker = new google.maps.Marker({
            position: latlng,
            map: map,
            icon: c.marker,
            title: c.title});
        c.infowindow = new google.maps.InfoWindow({content: c.content});
        google.maps.event.addListener(c.marker, 'click', makeCallback(c));
        // Create marker bounds
        markerBounds.extend(latlng);
    }
  // Create info windows based on above content
  function makeCallback(country) {
      return function () {
          country.infowindow.open(map, country.marker);
      };
  }
  // Fit map to marker boundaries
  map.fitBounds(markerBounds);
}
</script>

我基本上想从内容选项中删除任何空格,任何想法都值得赞赏!

这与空格无关。 这与内容字符串中过早结束字符串的未转义单引号有关。 这和其他JS语法错误将阻止谷歌地图工作。

您可以通过在前面

加上'来转义它们,例如尝试以下方法:

//Google map
function initialize() {
  var myOptions = {
    scrollwheel: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  // Create map on #map_canvas
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  // Define boundarys
  var markerBounds = new google.maps.LatLngBounds();
  var countries = [
    {title : 'M11 Moscow to St. Petersburg', lat : 55.938818, lon : 37.429733, marker : 'http://roadfree.org/images/map_markers/icon_map-marker.png', content : '<h4>M11 Moscow to St. Petersburg</h4><p>The M11 is a 650 km highway project aimed at connecting Moscow to Saint Petersburg. Despite several options proposed to build it, it was decided in 2009 that the road would run through Khimki Forest (between the 18 and 58 km marks), a centuries-old oak trees forest and host of many species of wildlife.</p><p>This decision provoked number of protests. On December 24 2011, nearly 100.000 persons rallied in Moscow, the largest demonstration in Russia since the fall of the Soviet Union.</p><p>The European Bank for Reconstruction and Development and the European Investment Bank, initially part of project, decided in 2011 to withdrawn their support.</p><p>The part of the road going through the forest is funded by private consortium including French giant construction group Vinci. Environmentalists and local residents denounce a case of corruption aimed at enriching property developers eager to increase housing facilities at the outskirts of Moscow.</p><p>The case of the M11 is also notorious because of the many reports of police brutality, intimidations, beatings and death of protesters and journalists.</p><p>The highway is due to open in 2014</p>'},
    {title : 'Title', lat : 1.576339, lon : 100.969849, marker : 'http://roadfree.org/images/map_markers/icon_map-marker_blue.png', content : '<h4>Title</h4><p>On the island of Sumatra in Indonesia, one of the world''s richest ecosystem and host of the critically endangered sumatran elephant, rhinoceros, tiger and orangutan, road-building through intact forests and protected areas is a recurrent theme.</p><p>Kerinci Seblat,</p><p>The 1.33m ha Kerinci Seblat National Park (KSNP) in West Sumatra, is strictly protected under Indonesian law and an ASEAN Heritage Site and a UNESCO World Heritage Site.</p><p>Despite being illegal, plans to open four roads (up to 12 meters wide) through the park have been regularly supported by Aceh''s consecutive governors for the last few years. For the local environmental NGOs denouncing the risks of fragmenting intact forest, destroying vulnerable ecosystems, and dividing breeding grounds and movement corridors, these plans are an aberration.</p><p>In June 2011, the Association for Tropical Biology and Conservation (ATBC) has drafted a resolution urging the Indonesian government to cancel plans to build roads through KSNP, the country''s oldest national park.</p><p>Aceh,</p><p>The Ladia-Galaska road network planned consists in building 450 kilometres of main road plus more than 1200 kilometres of minor roads. It would turn 2.5m ha of rainforest into a patchwork of forest islands (see GIS model picture). This project has been discussed for a decade. Under the influence of various NGOs and external political intervention from the European Union, the project has so far been kept at bay.</p><p>In both cases, the following patterns have been identified:</p><p>Constant spatial revision plans to convert protected forests in production forests.</p><p>- Existence of roads often legitimises these spatial revision plans. Once a road is built, the forest is opened to logging, mining and others industrial activities.</p><p>- Logic is missing: some roads do not connect to human population centres of sufficiently large size or will cut through watershed forest in areas already prone to severe flooding.</p><p>Viable alternatives to constructing new routes do exist. They would be better for the biodiversity and better for the local populations.</p>'},
    {title : 'Title of third movement', lat : 3, lon : 3, marker : 'http://roadfree.org/images/map_markers/icon_map-marker_green.png', content : '<h4>Title of third movement</h4><p>Donec sed odio dui. Fusce dapibus, tellus ac cursus commodo, tortor mauris…</p>'},
    {title : 'Title of fouth movement', lat : 24, lon : 52, marker : 'http://roadfree.org/images/map_markers/icon_map-marker_red.png', content : '<h4>Title of fouth movement</h4><p>Donec sed odio dui. Fusce dapibus, tellus ac cursus commodo, tortor mauris…</p>'},
    {title : 'Title of fifth movement', lat : -24, lon : -24, marker : 'http://roadfree.org/images/map_markers/icon_map-marker.png', content : '<h4>Title of fifth movement</h4><p>Donec sed odio dui. Fusce dapibus, tellus ac cursus commodo, tortor mauris…</p>'}
  ];
  // Create markers
  for (var i = 0; i < countries.length; i++) {
        var c = countries[i];
        var latlng = new google.maps.LatLng(c.lat, c.lon);
        c.marker = new google.maps.Marker({
            position: latlng,
            map: map,
            icon: c.marker,
            title: c.title});
        c.infowindow = new google.maps.InfoWindow({content: c.content});
        google.maps.event.addListener(c.marker, 'click', makeCallback(c));
        // Create marker bounds
        markerBounds.extend(latlng);
    }
  // Create info windows based on above content
  function makeCallback(country) {
      return function () {
          country.infowindow.open(map, country.marker);
      };
  }
  // Fit map to marker boundaries
  map.fitBounds(markerBounds);
}