使用json将infoWindow添加到地图标记

安德鲁·托卡(Andrew Tokar)

我有一个简单的脚本,可以在地图上添加标记,数据取自json。我想向所有标记添加一个基本的infoWindow,以便在单击它时显示“ Cartier”。您能告诉我InfoWindow代码在做什么错吗?代码如下。

<!DOCTYPE html>
    <html>
    <head>
    <style>
    html, body, #map-canvas {
      margin: 0;
      padding: 0;
      height: 100%;
    }
    </style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>

var map;

function initialize() {
  var mapOptions = {
        zoom: 2,
        center: {lat: 37.275, lng: 22.549},
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };

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

  // Create a <script> tag and set the USGS URL as the source.
  var script = document.createElement('script');

  script.src = 'http://pastebin.com/raw.php?i=7X956uB3';
  document.getElementsByTagName('head')[0].appendChild(script);

  map.data.setStyle(function(feature) {
    var jstores = feature.getProperty('jstores');
    return {
      icon: getCircle(jstores),
      title: (jstores)
    };
  });

  var contentString = '<div id="content">Cartier</div>';

  var infowindow = new google.maps.InfoWindow({
        content: contentString
      });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
  });
}

function getCircle(jstores) {
  var circle = {
        path: google.maps.SymbolPath.CIRCLE,
        fillColor: 'red',
        fillOpacity: .2,
        scale: Math.sqrt(jstores) * 2,
        strokeColor: 'white',
        strokeWeight: .5
      };
  return circle;
}

function jewellery_stores(results) {
  map.data.addGeoJson(results);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
 <body>
   <div id="map-canvas"></div>
 </body>
</html>

样本JSON:

jewellery_stores({ "type": "FeatureCollection","features": [
{"type": "Feature","geometry": {"type": "Point", "coordinates": [139.730407, 35.70883]},"properties": {"jstores": 106 }},
{"type": "Feature","geometry": {"type": "Point", "coordinates": [37.615556, 55.752222]},"properties": {"jstores": 94 }},
{"type": "Feature","geometry": {"type": "Point", "coordinates": [2.3524282, 48.8564528]},"properties": {"jstores": 89 }},
{"type": "Feature","geometry": {"type": "Point", "coordinates": [55.277067, 25.176594]},"properties": {"jstores": 66 }},
{"type": "Feature","geometry": {"type": "Point", "coordinates": [-0.1276597, 51.5072759]},"properties": {"jstores": 64 }},
{"type": "Feature","geometry": {"type": "Point", "coordinates": [114.169551, 22.285261]},"properties": {"jstores": 63 }}]

预先谢谢你,安德烈

地理编码

您正在使用Google Maps Javascript API v3数据层

它支持单击侦听器,该侦听器允许您打开包含JSON中包含的属性的信息窗口。

map.data.addListener('click', function (e) {
    infowindow.setPosition(e.latLng);
    infowindow.setContent("hello world<br>jstores="+e.feature.getProperty("jstores")+"<br>"+e.latLng.toUrlValue(6));
    infowindow.open(map);
});

工作代码段:

var map;
var infowindow = new google.maps.InfoWindow({});

function initialize() {
  var mapOptions = {
    zoom: 2,
    center: {
      lat: 37.275,
      lng: 22.549
    },
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

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

  // Create a <script> tag and set the USGS URL as the source.
  var script = document.createElement('script');

  script.src = 'https://pastebin.com/raw.php?i=7X956uB3';
  document.getElementsByTagName('head')[0].appendChild(script);

  map.data.setStyle(function(feature) {
    var jstores = feature.getProperty('jstores');
    return {
      icon: getCircle(jstores),
      title: (jstores)
    };
  });
}

function getCircle(jstores) {
  var circle = {
    path: google.maps.SymbolPath.CIRCLE,
    fillColor: 'red',
    fillOpacity: 0.2,
    scale: Math.sqrt(jstores) * 2,
    strokeColor: 'white',
    strokeWeight: 0.5
  };
  return circle;
}

function jewellery_stores(results) {
  map.data.addGeoJson(results);
  map.data.addListener('click', function(e) {
    infowindow.setPosition(e.latLng);
    infowindow.setContent("hello world<br>jstores=" + e.feature.getProperty("jstores") + "<br>" + e.latLng.toUrlValue(6));
    infowindow.open(map);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
  body,
  #map-canvas {
    margin: 0;
    padding: 0;
    height: 100%;
  }
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将ID添加到Google地图标记

将地图标记添加到“打开图层” 6

将弹出窗口添加到谷歌地图标记

Android:将数字添加到地图标记

将Infowindow添加到数组标记,每5秒刷新一次Google地图

谷歌地图标记未添加到地图

使用Knockout.js使用数组方法将地图标记添加到Google Maps

自定义图像以添加到地图标记

错误:google.maps.number 不是构造函数,当使用 javascript 将旋转属性添加到谷歌地图中的图标或标记时

传单-单击将类添加到标记图标

谷歌地图; 将地点卡添加到标记

将参数从地图添加到URL标记

导航地图时将标记添加到Google Map

如何使用毕加索将图标添加到标记中?

如何使用OpenLayers 4.6.4将图标/标记添加到OSM映射

使用for循环将标记聚类器添加到Google地图吗?

使用 JS 文件将首都标记添加到传单地图

将 InfoWindow 添加到 FastPointOverlay - OsmDroid

Google Maps API v3将InfoWindow添加到每个标记

将标记添加到Android中的Google地图后,如何为标记添加标题

将地图添加到 JSON 对象以保存到 Firestore

Groovy:将 json 中的值添加到地图

将新行添加到JSON作为标记(传单)

Restfullyii将<link>标记添加到json响应

在Android的Mapbox地图上添加多个标记,无法将源添加到mapboxMap

使用MvvmCross将图标添加到TabLayout

使用地图将元素添加到数组?

使用 Python 将地图添加到 Firestore 文档

使用Dom将标记添加到XML文件(最小)