使用containsLocation和google.maps.Data.polygon [英] Using containsLocation with a google.maps.Data.polygon

查看:569
本文介绍了使用containsLocation和google.maps.Data.polygon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多边形功能的google.maps.Data图层:

  state = new google.maps.Data ); 
state.loadGeoJson('static / geojson / ga_state.geojson',{
idPropertyName:'name'
});
state.setStyle({
clickable:false,
visible:false,
});
state.setMap(map);

在这个特征集中是一个代表乔治亚州的多边形:

  ga = state.getFeatureById('Georgia')

我可以得到这个特性的几何结构:

$ $ $ $ $ $ $ $ $ $ gaGeom = ga.getGeometry

但是,当我将这些对象以及原始数组传递给google.maps.geometry.polygon.containsFeature()时,我收到一个对象不包含get()函数的错误:

  google.maps.geometry.poly.containsLocation p.getPosition(),ga)
Uncaught TypeError:b.get不是函数(...)

google.maps.geometry.poly.containsLocation(p.getPosition(),gaGeom )
Uncaught TypeError:b.get不是函数(...)

google.maps.geometry.poly.containsLocation(p.getPosition(),gaGeom.getArray())
未捕获TypeError:b.get不是函数(...)

如何获得一个googl e.maps.Data.Polygon要么转换为google.maps.Polygon,要么使用此函数?



编辑:



我已经找到一种方法来从google.maps.Data.Polygon构建google.maps.Polygon,如下所示:

  // gaGeom是数据层的feature.geometry 
poly = new google.maps.Polygon({paths:gaGeom.getAt(0).getArray()} )

但是,当然必须有更简洁的方式来构建google.maps.Polygon?

解决方案

google.maps.geometry.poly.containsLocation方法需要一点( google.maps.LatLng )和一个多边形( google.maps.Polygon )
$ b


containsLocation(point:LatLng,polygon:Polygon)

返回值:boolean
计算给定点是否位于指定的多边形内。





其中没有一个是google.maps.Polygon 。你可以从Array中创建一个google.maps.Polygon(正如我看到你在我写这篇文章时发现的那样)。 b
概念验证



code:

 函数初始化(){
var map = new google.maps.Map(
document.getElementById(map_canvas ),{
center:new google.maps.LatLng(37.4419,-122.1419),
zoom:13,
mapTypeId:google.maps.MapTypeId.ROADMAP
});
var state = new google.maps.Data();
var poly;
state.addListener('addfeature',function(evt){
if(evt.feature.getId()==Georgia){
var ga = state.getFeatureById('Georgia ');
var gaGeom = ga.getGeometry();
// gaGeom是数据层的feature.geometry
poly = new google.maps.Polygon({
路径:gaGeom.getAt(0).getArray(),
map:map,
clickable:false
});
}
});
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(map,'click',function(evt){
infoWindow.setPosition(evt.latLng);
if(google.maps.geometry.poly。 containsLocation(evt.latLng,poly)){
infoWindow.setContent(INSIDE POLY< br>+ evt.latLng.toUrlValue(6));
} else {
infoWindow.setContent (OUTSIDE POLY< br> + evt.latLng.toUrlValue(6));
}
infoWindow.open(map);
});

state.loadGeoJson(http://www.geocodezip.com/GeoJSON/gz_2010_us_040_00_500k.json.txt,{
idPropertyName:'NAME'
});
state.setStyle({
clickable:false,
visible:false,
});
state.setMap(map);

var geocoder = new google.maps.Geocoder();
geocoder.geocode({$ b $'address':乔治亚州
},函数(结果,状态){
if(status === google.maps.GeocoderStatus .OK){
map.fitBounds(results [0] .geometry.viewport);
} else {
alert('由于以下原因,地理编码失败:'+ status);
}
});
}
google.maps.event.addDomListener(window,load,initialize);


I have a google.maps.Data layer with a polygon feature:

state = new google.maps.Data();
state.loadGeoJson('static/geojson/ga_state.geojson', {
    idPropertyName: 'name'
});
state.setStyle({
    clickable: false,
    visible: false,
});
state.setMap(map);

Within this feature collection is a polygon representing the state of Georgia:

ga = state.getFeatureById('Georgia')

I can get the geometry of this feature:

gaGeom = ga.getGeometry()

But when I pass either of these objects and also the raw array to google.maps.geometry.polygon.containsFeature(), I get an error that the object does not contain the get() fuction:

google.maps.geometry.poly.containsLocation(p.getPosition(), ga)
Uncaught TypeError: b.get is not a function(…)

google.maps.geometry.poly.containsLocation(p.getPosition(), gaGeom)
Uncaught TypeError: b.get is not a function(…)

google.maps.geometry.poly.containsLocation(p.getPosition(), gaGeom.getArray())
Uncaught TypeError: b.get is not a function(…)

How can I get a google.maps.Data.Polygon to either convert to a google.maps.Polygon or to work with this function?

EDIT:

I have found a way to construct a google.maps.Polygon from a google.maps.Data.Polygon as:

//gaGeom is the feature.geometry from the data layer
poly = new google.maps.Polygon({paths:gaGeom.getAt(0).getArray()})

But surely there has to be a cleaner way to construct the google.maps.Polygon?

解决方案

The google.maps.geometry.poly.containsLocation method takes a point (a google.maps.LatLng) and a polygon (a google.maps.Polygon).

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

  • ga = state.getFeatureById('Georgia') returns a "feature"
  • gaGeom = ga.getGeometry() returns a "Geometry"
  • gaGeom.getArray() returns an "Array" of LinearRings

None of which is a google.maps.Polygon. You can make a google.maps.Polygon from the Array (as I see you have discovered while I wrote this).

proof of concept

code:

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var state = new google.maps.Data();
  var poly;
  state.addListener('addfeature', function(evt) {
    if (evt.feature.getId() == "Georgia") {
      var ga = state.getFeatureById('Georgia');
      var gaGeom = ga.getGeometry();
      //gaGeom is the feature.geometry from the data layer
      poly = new google.maps.Polygon({
        paths: gaGeom.getAt(0).getArray(),
        map: map,
        clickable: false
      });
    }
  });
  var infoWindow = new google.maps.InfoWindow();
  google.maps.event.addListener(map, 'click', function(evt) {
    infoWindow.setPosition(evt.latLng);
    if (google.maps.geometry.poly.containsLocation(evt.latLng, poly)) {
      infoWindow.setContent("INSIDE POLY<br>" + evt.latLng.toUrlValue(6));
    } else {
      infoWindow.setContent("OUTSIDE POLY<br>" + evt.latLng.toUrlValue(6));
    }
    infoWindow.open(map);
  });

  state.loadGeoJson("http://www.geocodezip.com/GeoJSON/gz_2010_us_040_00_500k.json.txt", {
    idPropertyName: 'NAME'
  });
  state.setStyle({
    clickable: false,
    visible: false,
  });
  state.setMap(map);

  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'address': "State of Georgia"
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      map.fitBounds(results[0].geometry.viewport);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
google.maps.event.addDomListener(window, "load", initialize);

这篇关于使用containsLocation和google.maps.Data.polygon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆