使用Google Maps API的可点击国家/地区

时间:2016-04-26 13:22:41

标签: google-maps google-maps-api-3

我刚刚开始涉足谷歌地图API,但我已经陷入困境。我正在寻找一种方法来使用谷歌地图重新创建this类型的地图。我需要删除所有标签,获取空白背景(我尝试使用地图样式,但这对我不起作用,下面是代码示例)并在我将鼠标悬停在它们上时点亮这些国家。

我的搜索中是否有任何可能对我有所帮助的教程可以帮助我,或者有人能指出我正确的方向吗? :)

#include <deque>

template<class T>
struct Node {
    std::deque<Node<T> > branches;
    T data;

    Node(const T& _data) : data(_data) {
    }

};

void dummy() {
    Node<float> test(5.0f);
}

修改 这就是地图的用途:我已经在我的生活中走了一段路,并希望能做更多的事情。自从我第一次出访以来,我一直在博客上。我现在已经开发了一个新的博客,我想让我可以点击的国家/地区点击后点击一个网址。理想的情况是,当我点击某个国家/地区时,会将我带到一个页面,其中只有该特定国家/地区会显示在带有某些标记的地图中(访问的地方)。点击这些标记后,它应显示特定的帖子/某些信息。

2 个答案:

答案 0 :(得分:19)

这样的东西可以吗?复制并粘贴到HTML网页正文(JSFiddler here)中。

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

<script type="text/javascript"
    src="https://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">
  // the map
  var map;

  function initialize() {
    var myOptions = {
      zoom: 2,
      center: new google.maps.LatLng(10, 0),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    // initialize the map
    map = new google.maps.Map(document.getElementById('map-canvas'),
        myOptions);

    // these are the map styles
    var styles = [
        {
          stylers: [
            { hue: "#00ffe6" },
            { saturation: -20 }
          ]
        },
        {
          featureType: "landscape",
          stylers: [
            { hue: "#ffff66" },
            { saturation: 100 }
          ]
        },{
          featureType: "road",
          stylers: [
            { visibility: "off" }
          ]
        },{
          featureType: "administrative.land_parcel",
          stylers: [
            { visibility: "off" }
          ]
        },{
          featureType: "administrative.locality",
          stylers: [
            { visibility: "off" }
          ]
        },{
          featureType: "administrative.neighborhood",
          stylers: [
            { visibility: "off" }
          ]
        },{
          featureType: "administrative.province",
          stylers: [
            { visibility: "off" }
          ]
        },{
          featureType: "landscape.man_made",
          stylers: [
            { visibility: "off" }
          ]
        },{
          featureType: "landscape.natural",
          stylers: [
            { visibility: "off" }
          ]
        },{
          featureType: "poi",
          stylers: [
            { visibility: "off" }
          ]
        },{
          featureType: "transit",
          stylers: [
            { visibility: "off" }
          ]
        }
      ];

    map.setOptions({styles: styles});

    // Initialize JSONP request
    var script = document.createElement('script');
    var url = ['https://www.googleapis.com/fusiontables/v1/query?'];
    url.push('sql=');
    var query = 'SELECT name, kml_4326 FROM ' +
        '1foc3xO9DyfSIF6ofvN0kp2bxSfSeKog5FbdWdQ';
    var encodedQuery = encodeURIComponent(query);
    url.push(encodedQuery);
    url.push('&callback=drawMap');
    url.push('&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ');
    script.src = url.join('');
    var body = document.getElementsByTagName('body')[0];
    body.appendChild(script);
  }

  function drawMap(data) {
    var rows = data['rows'];
    for (var i in rows) {
      if (rows[i][0] != 'Antarctica') {
        var newCoordinates = [];
        var geometries = rows[i][1]['geometries'];
        if (geometries) {
          for (var j in geometries) {
            newCoordinates.push(constructNewCoordinates(geometries[j]));
          }
        } else {
          newCoordinates = constructNewCoordinates(rows[i][1]['geometry']);
        }
        var country = new google.maps.Polygon({
          paths: newCoordinates,
          strokeColor: '#ff9900',
          strokeOpacity: 1,
          strokeWeight: 0.3,
          fillColor: '#ffff66',
          fillOpacity: 0,
          name: rows[i][0]
        });
        google.maps.event.addListener(country, 'mouseover', function() {
          this.setOptions({fillOpacity: 0.4});
        });
        google.maps.event.addListener(country, 'mouseout', function() {
          this.setOptions({fillOpacity: 0});
        });
        google.maps.event.addListener(country, 'click', function() {
          alert(this.name);
        });

        country.setMap(map);
      }
    }
  }

  function constructNewCoordinates(polygon) {
    var newCoordinates = [];
    var coordinates = polygon['coordinates'][0];
    for (var i in coordinates) {
      newCoordinates.push(
          new google.maps.LatLng(coordinates[i][1], coordinates[i][0]));
    }
    return newCoordinates;
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

<div id="map-canvas"></div>

这是此版本的修改版本 Fusion Tables Layer Example: Mouseover Map Styles

您还应该查看Styled Maps

您可能感兴趣的另一件事是Natural Earth Data。在这种情况下,您需要一个多边形数据源。这里有一个使用它的例子GViz API Example: Fusion Tables Data Source

答案 1 :(得分:1)

不推荐使用融合表,因此没有内置的google解决方案。

这是我编写的小示例代码:

https://github.com/arturssmirnovs/Clickable-countries-using-Google-Maps-API

和此处的工作示例:https://arturssmirnovs.github.io/Clickable-countries-using-Google-Maps-API/json/