草庐IT

javascript - Google Maps 3 API - 单击功能(来自 geojson)并检查它是否包含位置

coder 2025-03-24 原文

我已经在我的 map 上成功加载了一个 geojson 文件。我可以单击每个多边形来更改笔划并访问其属性。但我想知道某些点是否在每个多边形内。

我已将 google.maps.geometry.poly.containsLocation() 用于法线多边形。有没有一种方法可以从 event.feature.getGeometry()...

map.data.loadGeoJson('inc-tracts.json');
var featureStyle = {
    strokeColor: '#000000',
    strokeOpacity: 0.5,
    strokeWeight: 3,
}
map.data.setStyle(featureStyle);
map.data.addListener('click', function(event) {
    console.log(event.feature.getProperty("CTNAME"));
    // This is where I want to check if point(s) fall within it.
}

最佳答案

google.maps.data.Polygon 不是 google.maps.Polygongoogle.maps.geometry.poly.containsLocation 方法有两个参数:


Methods                                         Return Value    Description
containsLocation(point:LatLng, polygon:Polygon) boolean         Computes whether the given point lies inside the specified polygon.

A google.maps.LatLng object and a google.maps.Polygon (not a google.maps.data.Polygon). You need to create a google.maps.Polygon from the data in the google.maps.data.Polygon to use that function.

working fiddle

proof of concept code snippet:

function initialize() {
  // Create a simple map.
  features = [];
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 4,
    center: {
      lat: -28,
      lng: 137.883
    }
  });
  var markers = [
    [31.713127, 35.206804],
    [31.712762, 35.22028],
    [31.706117, 35.210753],
    [31.717216, 35.210066],
    [31.701152, 35.188265],
    [31.704073, 35.19144]
  ];
  var gmarkers = [];
  var info = new google.maps.InfoWindow();
  for (var i = 0; i < markers.length; i++) {
    gmarkers.push(new google.maps.Marker({
      position: {
        lat: markers[i][0],
        lng: markers[i][1]
      },
      draggable: true,
      map: map
    }));
    google.maps.event.addListener(gmarkers[gmarkers.length - 1], 'click', function() {
      info.setContent(this.getPosition().toUrlValue(6));
      info.open(map, this);
    });
  }

  google.maps.event.addListener(map, 'click', function(e) {
    document.getElementById('info').innerHTML += e.latLng.toUrlValue(6) + "<br>";
  })
  google.maps.event.addListener(map, 'click', function() {
    info.close();
  });
  // process the loaded GeoJSON data.
  var bounds = new google.maps.LatLngBounds();
  google.maps.event.addListener(map.data, 'addfeature', function(e) {
    if (e.feature.getGeometry().getType() === 'Polygon') {
      var polys = e.feature.getGeometry().getArray();
      for (var i = 0; i < polys.length; i++) {
        for (var j = 0; j < polys[i].getLength(); j++) {
          bounds.extend(polys[i].getAt(j));
        }
      }
      map.fitBounds(bounds);
    }
  });
  map.data.addGeoJson(data);
  map.data.addListener('click', function(event) {
    if (event.feature.getGeometry().getType() === 'Polygon') {
      var polyPath = event.feature.getGeometry().getAt(0).getArray();
      var poly = new google.maps.Polygon({
        paths: polyPath
      });
      for (var i = 0; i < gmarkers.length; i++) {
        if (google.maps.geometry.poly.containsLocation(gmarkers[i].getPosition(), poly)) {
          gmarkers[i].setIcon("http://maps.google.com/mapfiles/ms/icons/blue.png");
        } else {
          gmarkers[i].setIcon("");
        }
      }
    };
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
var data = {
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "properties": {
      "name": "test",
      "desc": "test desc",
      "inDailyProgram": true,
      "color": "red"
    },
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [
            35.188327,
            31.699929,
            0
          ],
          [
            35.187895,
            31.699669,
            0
          ],
          [
            35.187014,
            31.699729,
            0
          ],
          [
            35.186867,
            31.700016,
            0
          ],
          [
            35.186783,
            31.700395,
            0
          ],
          [
            35.186921,
            31.700787,
            0
          ],
          [
            35.187232,
            31.701291,
            0
          ],
          [
            35.18763,
            31.701844,
            0
          ],
          [
            35.187442,
            31.702328,
            0
          ],
          [
            35.18692,
            31.702779,
            0
          ],
          [
            35.187064,
            31.703654,
            0
          ],
          [
            35.187433,
            31.703794,
            0
          ],
          [
            35.188155,
            31.70344,
            0
          ],
          [
            35.188921,
            31.702917,
            0
          ],
          [
            35.189348,
            31.702887,
            0
          ],
          [
            35.189828,
            31.70302,
            0
          ],
          [
            35.190313,
            31.703443,
            0
          ],
          [
            35.190359,
            31.704104,
            0
          ],
          [
            35.190224,
            31.704348,
            0
          ],
          [
            35.189797,
            31.704585,
            0
          ],
          [
            35.189753,
            31.704948,
            0
          ],
          [
            35.189847,
            31.705107,
            0
          ],
          [
            35.190187,
            31.705015,
            0
          ],
          [
            35.190604,
            31.705041,
            0
          ],
          [
            35.190931,
            31.705171,
            0
          ],
          [
            35.191435,
            31.70526,
            0
          ],
          [
            35.191861,
            31.705231,
            0
          ],
          [
            35.192482,
            31.705008,
            0
          ],
          [
            35.192945,
            31.704893,
            0
          ],
          [
            35.193564,
            31.704449,
            0
          ],
          [
            35.192869,
            31.704004,
            0
          ],
          [
            35.192256,
            31.703737,
            0
          ],
          [
            35.191754,
            31.703371,
            0
          ],
          [
            35.191306,
            31.703001,
            0
          ],
          [
            35.190838,
            31.702632,
            0
          ],
          [
            35.190546,
            31.70221,
            0
          ],
          [
            35.190348,
            31.701739,
            0
          ],
          [
            35.190323,
            31.701589,
            0
          ],
          [
            35.189814,
            31.701624,
            0
          ],
          [
            35.189443,
            31.701456,
            0
          ],
          [
            35.189108,
            31.701217,
            0
          ],
          [
            35.188509,
            31.700359,
            0
          ],
          [
            35.188327,
            31.699929,
            0
          ]
        ]
      ]
    }
  }, {
    "type": "Feature",
    "properties": {
      "name": "test",
      "desc": "test desc",
      "inDailyProgram": true,
      "color": "red"
    },
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [
            35.209224,
            31.718497,
            0
          ],
          [
            35.209775,
            31.718459,
            0
          ],
          [
            35.210282,
            31.717788,
            0
          ],
          [
            35.210741,
            31.71723,
            0
          ],
          [
            35.21132,
            31.716803,
            0
          ],
          [
            35.211748,
            31.716193,
            0
          ],
          [
            35.212124,
            31.715632,
            0
          ],
          [
            35.212315,
            31.714798,
            0
          ],
          [
            35.21227,
            31.714137,
            0
          ],
          [
            35.212647,
            31.713599,
            0
          ],
          [
            35.21316,
            31.713412,
            0
          ],
          [
            35.214134,
            31.713095,
            0
          ],
          [
            35.215018,
            31.712675,
            0
          ],
          [
            35.215822,
            31.7119,
            0
          ],
          [
            35.21577,
            31.711156,
            0
          ],
          [
            35.215564,
            31.710175,
            0
          ],
          [
            35.215104,
            31.709128,
            0
          ],
          [
            35.21473,
            31.708518,
            0
          ],
          [
            35.214301,
            31.707911,
            0
          ],
          [
            35.214086,
            31.707207,
            0
          ],
          [
            35.213709,
            31.706154,
            0
          ],
          [
            35.213519,
            31.705807,
            0
          ],
          [
            35.212415,
            31.705441,
            0
          ],
          [
            35.211313,
            31.705103,
            0
          ],
          [
            35.210114,
            31.704964,
            0
          ],
          [
            35.20915,
            31.705031,
            0
          ],
          [
            35.208402,
            31.704612,
            0
          ],
          [
            35.207939,
            31.704119,
            0
          ],
          [
            35.207657,
            31.704636,
            0
          ],
          [
            35.207123,
            31.704922,
            0
          ],
          [
            35.206421,
            31.705164,
            0
          ],
          [
            35.205969,
            31.70529,
            0
          ],
          [
            35.205926,
            31.705613,
            0
          ],
          [
            35.205553,
            31.705808,
            0
          ],
          [
            35.205577,
            31.706157,
            0
          ],
          [
            35.205694,
            31.706459,
            0
          ],
          [
            35.205902,
            31.70686,
            0
          ],
          [
            35.206285,
            31.707193,
            0
          ],
          [
            35.206113,
            31.707375,
            0
          ],
          [
            35.206005,
            31.707544,
            0
          ],
          [
            35.206139,
            31.707746,
            0
          ],
          [
            35.206713,
            31.708194,
            0
          ],
          [
            35.207228,
            31.708428,
            0
          ],
          [
            35.207474,
            31.708798,
            0
          ],
          [
            35.207463,
            31.709435,
            0
          ],
          [
            35.207359,
            31.709878,
            0
          ],
          [
            35.207255,
            31.710418,
            0
          ],
          [
            35.207232,
            31.71089,
            0
          ],
          [
            35.20712,
            31.711257,
            0
          ],
          [
            35.206802,
            31.711473,
            0
          ],
          [
            35.206408,
            31.711645,
            0
          ],
          [
            35.206061,
            31.711753,
            0
          ],
          [
            35.205639,
            31.711857,
            0
          ],
          [
            35.205398,
            31.711766,
            0
          ],
          [
            35.205213,
            31.711698,
            0
          ],
          [
            35.205289,
            31.711992,
            0
          ],
          [
            35.205266,
            31.712464,
            0
          ],
          [
            35.205096,
            31.712808,
            0
          ],
          [
            35.204885,
            31.713348,
            0
          ],
          [
            35.204829,
            31.71414,
            0
          ],
          [
            35.204947,
            31.714644,
            0
          ],
          [
            35.205089,
            31.715104,
            0
          ],
          [
            35.205489,
            31.715687,
            0
          ],
          [
            35.205906,
            31.716113,
            0
          ],
          [
            35.206464,
            31.716586,
            0
          ],
          [
            35.20684,
            31.716421,
            0
          ],
          [
            35.207254,
            31.716005,
            0
          ],
          [
            35.207524,
            31.715517,
            0
          ],
          [
            35.207901,
            31.714965,
            0
          ],
          [
            35.207949,
            31.714464,
            0
          ],
          [
            35.208022,
            31.713919,
            0
          ],
          [
            35.20835,
            31.713855,
            0
          ],
          [
            35.208542,
            31.714229,
            0
          ],
          [
            35.208847,
            31.71465,
            0
          ],
          [
            35.209151,
            31.715044,
            0
          ],
          [
            35.20929,
            31.71545,
            0
          ],
          [
            35.209362,
            31.715694,
            0
          ],
          [
            35.209315,
            31.716214,
            0
          ],
          [
            35.209177,
            31.716619,
            0
          ],
          [
            35.209031,
            31.716906,
            0
          ],
          [
            35.208958,
            31.717132,
            0
          ],
          [
            35.208853,
            31.717333,
            0
          ],
          [
            35.208878,
            31.717691,
            0
          ],
          [
            35.209224,
            31.718497,
            0
          ]
        ]
      ]
    }
  }]
};
html,
body {
  height: 100%;
  margin: 0px;
  padding: 0px;
  width: 100%;
}
#map-canvas {
  height: 100%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id='info'></div>
<div id="map-canvas"></div>

关于javascript - Google Maps 3 API - 单击功能(来自 geojson)并检查它是否包含位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29309856/

有关javascript - Google Maps 3 API - 单击功能(来自 geojson)并检查它是否包含位置的更多相关文章

  1. ruby-on-rails - ActionController::RoutingError: 未初始化常量 Api::V1::ApiController - 2

    我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc

  2. 【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式 - 2

    在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList​()Obt

  3. ruby-on-rails - Mandrill API 模板 - 2

    我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h

  4. ruby-on-rails - 在 Ruby (on Rails) 中使用 imgur API 获取图像 - 2

    我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path

  5. ruby-on-rails - 使用 HTTParty 的非常基本的 Rails 4.1 API 调用 - 2

    Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"

  6. ruby-on-rails - 是否使用 API - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我的公司有一个巨大的数据库,该数据库接收来自多个来源的(许多)事件,用于监控和报告目的。到目前为止,数据中的每个新仪表板或图形都是一个新的Rails应用程序,在巨大的数据库中有额外的表,并且可以完全访问数据库内容。最近,有一个想法让外部(不是我们公司,而是姊妹公司)客户访问我们的数据,并且决定我们应该公开一个只读的RESTfulAPI来查询我们的数据。我的观点是-我们是否也应该为我们的自己

  7. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  8. ruby - Ruby 中的必应搜索 API - 2

    我读了"BingSearchAPI-QuickStart"但我不知道如何在Ruby中发出这个http请求(Weary)如何在Ruby中翻译“Stream_context_create()”?这是什么意思?"BingSearchAPI-QuickStart"我想使用RubySDK,但我发现那些已被弃用前(Rbing)https://github.com/mikedemers/rbing您知道Bing搜索API的最新包装器(仅限Web的结果)吗? 最佳答案 好吧,经过一个小时的挫折,我想出了一个办法来做到这一点。这段代码很糟糕,因为它是

  9. python - 用于 Python 或 Ruby 的 Amazon Book API? - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:AmazonAPIlibraryforPython?我正在寻找一个AmazonAPI,它可以让我:按书名或作者查找书籍显示书籍封面获取有关每本书的信息(价格、评级、评论数、格式、页数等)Python或Ruby库都可以(我只想要最容易使用的库)。有什么建议么?我知道在SO上还有其他一些关于此的帖子,但这些API似乎很快就过时了。[几个月前我尝试了几个建议的Ruby库,但无法让它们中的任何一个工作。]

  10. ruby - Google-api-ruby-client 翻译 API 示例 - 2

    很高兴看到google代码:google-api-ruby-client项目,因为这对我来说意味着Ruby人员可以使用GoogleAPI-s来完善代码。虽然我现在很困惑,因为给出的唯一示例使用Buzz,并且根据我的实验,Google翻译(v2)api的行为必须与google-api-ruby-client中的Buzz完全不同。.我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个探索器。它所做的只是调用一个Buzz服务,然后浏览它已经知道的关于Buzz服务的事情。对我来说,Explorer应该让您“发现”所公开的服务和方法/功能,而不一定已经知道它们。我很想听听使用这个

随机推荐