google-maps - 使 d3 svg 路径可点击(在 Google map 叠加层中)

标签 google-maps d3.js

我正在尝试使用 d3 在 Google map 上绘制一堆多边形。该部分有效(感谢 StackOverflow 上每个人的代码示例!)。

我提供了一个 GeoJSON 多边形列表,d3+Google map 叠加层发挥了它的魔力。

现在我想让多边形可点击,这样我就可以打开信息窗口之类的东西并显示 GeoJSON 标签。

我尝试添加“.attr("onclick","alert('click');")"以及各种形式的内容,但它不起作用,因为 Google map 接收的是点击而不是叠加层。 ..

关于使个人可点击有什么想法吗?

My working example

<html>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/d3/2.10.0/d3.v2.min.js"></script>

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

<style>

    #map {
        width: 100%;
        height: 100%;
    }

    .SvgOverlay {
        position: relative;
        width: 100%;
        height: 100%;           
    }

    .SvgOverlay svg {
        position: absolute;
        top: -4000px;
        left: -4000px;
        width: 8000px;
        height: 8000px;        
    }

    .SvgOverlay path {
        stroke: Orange;
        stroke-width: 2px;
        fill: Orange;
        fill-opacity: .3;
    }

</style>

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

<script>

    $(function () {

        var $map = $("#map");

        var map = new google.maps.Map($map[0], {
                zoom: 12,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: {lat:33.045579573575154, lng:-96.97189523828126}         
            });

        var geoJson = myJson();

        var overlay = new google.maps.OverlayView();

        overlay.onAdd = function () {
            var layer = d3.select(this.getPanes().overlayLayer).append("div").attr("class", "SvgOverlay").attr("id","mySvg");
            var svg = layer.append("svg");
            var adminDivisions = svg.append("g").attr("class", "AdminDivisions");

            overlay.draw = function () {
                var markerOverlay = this;
                var overlayProjection = markerOverlay.getProjection();

                // Turn the overlay projection into a d3 projection
                var googleMapProjection = function (coordinates) {
                    var googleCoordinates = new google.maps.LatLng(coordinates[1], coordinates[0]);
                    var pixelCoordinates = overlayProjection.fromLatLngToDivPixel(googleCoordinates);
                    return [pixelCoordinates.x + 4000, pixelCoordinates.y + 4000];
                }

                path = d3.geo.path().projection(googleMapProjection);

                adminDivisions.selectAll("path")
                    .data(geoJson.features)
                    .attr("d", path) // update existing paths
                    .attr("class","myPathClass")
                    .enter().append("svg:path")
                    .attr("d", path);

            };

            //DOESN'T WORK  :(
                //Try adding click event to main-<svg>
                d3.selectAll(".SvgOverlay").on("click", doSomething);

                //Try adding click event to individual <path>
                d3.selectAll(".myPathClass").on("click", doSomething);
        };

        overlay.setMap(map);

        function doSomething(){
            alert("Clicked");
        }

        function myJson() {
            return {                    
            "type": "FeatureCollection",
            "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
            "features": [
                { "type": "Feature", "properties": { "Name": "Autumn Breeze", "Description": "<html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"> <head> <META http-equiv=\"Content-Type\" content=\"text\/html\"> <meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\"> <\/head> <body style=\"margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;\"> <table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px\"> <tr style=\"text-align:center;font-weight:bold;background:#9CBCE2\"> <td>Autumn Breeze<\/td> <\/tr> <tr> <td> <table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px\"> <tr> <td>OBJECTID_1<\/td> <td>1<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Id<\/td> <td>4<\/td> <\/tr> <tr> <td>Apt_Name<\/td> <td>Autumn Breeze<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Apt_Addr<\/td> <td>1679 S. Highway 121 Business<\/td> <\/tr> <tr> <td>GlobalID<\/td> <td>{630B0681-BC80-4C21-960B-EA9E2F9F194B}<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Lat<\/td> <td>33.0206260437915<\/td> <\/tr> <tr> <td>Lng<\/td> <td>-96.9952137886204<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Shape.STArea()<\/td> <td>526542.164207<\/td> <\/tr> <tr> <td>Shape.STLength()<\/td> <td>3392.783995<\/td> <\/tr> <\/table> <\/td> <\/tr> <\/table> <\/body> <\/html>" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -96.99731320352906, 33.021216819164927, 0.0 ], [ -96.997317696301693, 33.02009550984215, 0.0 ], [ -96.993114621094179, 33.020032531562563, 0.0 ], [ -96.993117243000668, 33.021160055075534, 0.0 ], [ -96.99731320352906, 33.021216819164927, 0.0 ] ] ] ] } },
                { "type": "Feature", "properties": { "Name": "Ballantyne", "Description": "<html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"> <head> <META http-equiv=\"Content-Type\" content=\"text\/html\"> <meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\"> <\/head> <body style=\"margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;\"> <table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px\"> <tr style=\"text-align:center;font-weight:bold;background:#9CBCE2\"> <td>Ballantyne<\/td> <\/tr> <tr> <td> <table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px\"> <tr> <td>OBJECTID_1<\/td> <td>2<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Id<\/td> <td>5<\/td> <\/tr> <tr> <td>Apt_Name<\/td> <td>Ballantyne<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Apt_Addr<\/td> <td>2801 Denton Tap Road<\/td> <\/tr> <tr> <td>GlobalID<\/td> <td>{55637E1D-E20A-4612-91FF-2C642579B548}<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Lat<\/td> <td>32.9922107728575<\/td> <\/tr> <tr> <td>Lng<\/td> <td>-96.9903318258401<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Shape.STArea()<\/td> <td>931629.409071<\/td> <\/tr> <tr> <td>Shape.STLength()<\/td> <td>4475.375089<\/td> <\/tr> <\/table> <\/td> <\/tr> <\/table> <\/body> <\/html>" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -96.993073223213301, 32.993015759955497, 0.0 ], [ -96.993052456415995, 32.992954231928493, 0.0 ], [ -96.9930440698203, 32.992592596409807, 0.0 ], [ -96.993050014349279, 32.992243389503017, 0.0 ], [ -96.99306841265475, 32.992016887012817, 0.0 ], [ -96.993093976413959, 32.991796598964598, 0.0 ], [ -96.993106356223819, 32.991496491331851, 0.0 ], [ -96.987610939012924, 32.991399297910839, 0.0 ], [ -96.987592146800381, 32.992924867973123, 0.0 ], [ -96.990485142326506, 32.992984445801007, 0.0 ], [ -96.993073223213301, 32.993015759955497, 0.0 ] ] ] ] } },
                { "type": "Feature", "properties": { "Name": "Catalina", "Description": "<html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"> <head> <META http-equiv=\"Content-Type\" content=\"text\/html\"> <meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\"> <\/head> <body style=\"margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;\"> <table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px\"> <tr style=\"text-align:center;font-weight:bold;background:#9CBCE2\"> <td>Catalina<\/td> <\/tr> <tr> <td> <table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px\"> <tr> <td>OBJECTID_1<\/td> <td>3<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Id<\/td> <td>14<\/td> <\/tr> <tr> <td>Apt_Name<\/td> <td>Catalina<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Apt_Addr<\/td> <td>998 Bellaire Boulevard<\/td> <\/tr> <tr> <td>GlobalID<\/td> <td>{7D11055C-6487-44A5-8C08-88DD63F2AF00}<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Lat<\/td> <td>33.0263437562578<\/td> <\/tr> <tr> <td>Lng<\/td> <td>-97.0139839544527<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Shape.STArea()<\/td> <td>444373.831028<\/td> <\/tr> <tr> <td>Shape.STLength()<\/td> <td>2703.799415<\/td> <\/tr> <\/table> <\/td> <\/tr> <\/table> <\/body> <\/html>" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -97.014838611955057, 33.027467357582204, 0.0 ], [ -97.014882993690847, 33.0252088650658, 0.0 ], [ -97.013270995985607, 33.025156916939082, 0.0 ], [ -97.013039468436816, 33.026600148499, 0.0 ], [ -97.013063841670515, 33.027462577753482, 0.0 ], [ -97.014838611955057, 33.027467357582204, 0.0 ] ] ] ] } },
                { "type": "Feature", "properties": { "Name": "Hampton Bay", "Description": "<html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"> <head> <META http-equiv=\"Content-Type\" content=\"text\/html\"> <meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\"> <\/head> <body style=\"margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;\"> <table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px\"> <tr style=\"text-align:center;font-weight:bold;background:#9CBCE2\"> <td>Hampton Bay<\/td> <\/tr> <tr> <td> <table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px\"> <tr> <td>OBJECTID_1<\/td> <td>4<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Id<\/td> <td>32<\/td> <\/tr> <tr> <td>Apt_Name<\/td> <td>Hampton Bay<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Apt_Addr<\/td> <td>248 East Southwest Parkway<\/td> <\/tr> <tr> <td>GlobalID<\/td> <td>{2D27BDA7-0CCD-46C3-ADD0-E1EB8C941318}<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Lat<\/td> <td>33.0180338579138<\/td> <\/tr> <tr> <td>Lng<\/td> <td>-96.9901980684424<\/td> <\/tr> <tr bgcolor=\"#D4E4F3\"> <td>Shape.STArea()<\/td> <td>378542.166679<\/td> <\/tr> <tr> <td>Shape.STLength()<\/td> <td>2489.966375<\/td> <\/tr> <\/table> <\/td> <\/tr> <\/table> <\/body> <\/html>" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -96.989455025454433, 33.01699540133685, 0.0 ], [ -96.989422131289444, 33.017225696133558, 0.0 ], [ -96.98940314831394, 33.01906784144068, 0.0 ], [ -96.990007468787724, 33.019069912294732, 0.0 ], [ -96.991054060203666, 33.019077333273032, 0.0 ], [ -96.991075837766445, 33.017434680867211, 0.0 ], [ -96.989776927723966, 33.016686809835257, 0.0 ], [ -96.989455025454433, 33.01699540133685, 0.0 ] ] ] ] } },
            ]}
        } //end function

    });

</script>

最佳答案

这是我发现的:您需要将overlayLayer更改为overlayMouseTarget。这是代码:

var overlay = new google.maps.OverlayView();
overlay.onAdd = function() {
    var layer = d3.select(this.getPanes().overlayMouseTarget)
        .append("div")
        .attr("class", "providers");
         ...
}

这对我有用。这是原始帖子:link .

关于google-maps - 使 d3 svg 路径可点击(在 Google map 叠加层中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22411088/

相关文章:

javascript - 引用错误 : google is not defined using ng-map GeoCoder service

android - Google Map API 使线条在弯曲时更平滑

javascript - 获取 map google div 中的 DOM 元素

javascript - 具有双 y 轴的 Angular NVD3 Multibar Chart 使用 json 数据仅显示线

javascript - 替代 d3.scaleQuantize?

javascript - D3 从纬度和经度寻找路径

android - 标准 Android Geocoder 使用限制

javascript - 我可以使用 Javascript 获取文件目录列表吗?

d3.js - dc.js - 动态更改折线图中堆叠层的 valueAccessor 并重绘它

android - 折线宽度转换 map api v2