【问题标题】:Importing Google Maps Polygon Extension导入谷歌地图多边形扩展
【发布时间】:2022-01-02 22:16:05
【问题描述】:

我希望在我的 React 项目中导入一个多边形扩展,但我不确定如何去做。

这就是我正在查看的内容:https://github.com/openplacedatabase/gmaps-large-polygons

我不知道该怎么做,我已经将包含多边形原型扩展的 .js 文件导入到我的项目中,并在我的 index.html 中添加了一行在导入 Google 地图之前。

<script type="text/javascript" src="../utils/gmaps-large-polygons/gmaps-large-polygons.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=%REACT_APP_G_MAP_API%&libraries=geometry,visualization,places,drawing,directions"></script>

例如,在创建像 lineSize 这样的多边形时,我尝试使用新选项,但它似乎不起作用。

我想肯定还有一些我不知道的步骤。

【问题讨论】:

    标签: javascript google-maps-api-3


    【解决方案1】:
    1. 首先导入 Google Maps Javascript API v3(因此多边形已定义并可扩展)
    2. 导入gmaps-large-polygons 脚本
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&v=weekly&channel=2"></script>
    <script src="https://cdn.jsdelivr.net/gh/openplacedatabase/gmaps-large-polygons@master/gmaps-large-polygon.js"></script>
    

    proof of concept fiddle

    代码 sn-p:

    var poly;
    // This example is the demo from https://github.com/openplacedatabase/gmaps-large-polygons
    function initMap() {
      /**
       * http://stackoverflow.com/a/6339384/879121
       */
      google.maps.Polygon.prototype.getBounds = function() {
        var bounds = new google.maps.LatLngBounds();
        var paths = this.getPaths();
        var path;
        for (var i = 0; i < paths.getLength(); i++) {
          path = paths.getAt(i);
          for (var ii = 0; ii < path.getLength(); ii++) {
            bounds.extend(path.getAt(ii));
          }
        }
        return bounds;
      };
      var mapOptions = {
        center: new google.maps.LatLng(20, -10),
        zoom: 3,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
    
      var shapeStyle = {
        "strokeColor": "#428bca",
        "strokeOpacity": 1,
        "strokeWeight": 3,
        "fillColor": "#9999ff",
        "fillOpacity": 0.3,
        editable: true,
        highlightColor: '#111111'
      };
    
    
      // Create map
      var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    
      //
      // Setup polygon
      //
    
      // Create a polygon with a lot of points and add it to the map
      poly = google.maps.geojson.from(geojsonPolygon);
      poly.setMap(map);
      poly.setOptions(shapeStyle);
      map.fitBounds(poly.getBounds());
    
      // Make the polygon editable when clicked
    
    
    }
    google.maps.event.addDomListener(window, 'load', initMap);
    var geojsonPolygon = {
      "type": "Polygon",
      "coordinates": [
        [
          [-87.71570900046997,
            33.00682400017906
          ],
    /* snip so fit in code snippet */
          [-87.83205499982405,
            32.76975599977849
          ],
          [-87.8308639998843,
            32.76963799981476
          ],
          [-87.830633000212,
            32.76961700016936
          ],
          [-87.83020500023788,
            32.76957799987701
          ],
          [-87.8285420005035,
            32.768902999699705
          ],
          [-87.82809699992815,
            32.76872100038072
          ],
          [-87.82586399971979,
            32.76760800015759
          ],
          [-87.82531199994148,
            32.76733300039403
          ],
          [-87.82512100027712,
            32.76720999973664
          ],
          [-87.82474599961421,
            32.76695199994133
          ],
          [-87.82447100018686,
            32.76676699986933
          ],
          [-87.82317599986355,
            32.76589500033653
          ],
          [-87.82101100011603,
            32.763795999921896
          ],
          [-87.81651100016283,
            32.75942800023396
          ],
          [-87.81604499980914,
            32.75902199973601
          ],
          [-87.81538000013052,
            32.75844000040231
          ],
          [-87.81471500019546,
            32.75785900000458
          ],
          [-87.81444500020118,
            32.75762099989485
          ],
          [-87.81417399974622,
            32.757384999743635
          ],
          [-87.8123259996751,
            32.75577099978005
          ],
          [-87.80921399979633,
            32.753154000321935
          ],
          [-87.80911399972612,
            32.753069999896475
          ],
          [-87.80901300002292,
            32.75298499961307
          ],
          [-87.80828399988305,
            32.75237200015667
          ],
          [-87.80725500046118,
            32.75130599967815
          ],
          [-87.80651199996353,
            32.75013099979255
          ],
          [-87.8053010000872,
            32.74784400019017
          ],
          [-87.80429899960293,
            32.74554999970874
          ],
          [-87.80407099975815,
            32.74482700001399
          ],
          [-87.80434100044876,
            32.74292800003545
          ],
          [-87.80642999970335,
            32.73739499991951
          ],
          [-87.80670899975426,
            32.73619000022151
          ],
          [-87.80707099995972,
            32.734791999730405
          ],
          [-87.80732100031263,
            32.73407099968153
          ],
          [-87.80741099962275,
            32.733811999930865
          ],
          [-87.80751700057645,
            32.73371999959244
          ],
          [-87.80818299974251,
            32.73314200004328
          ],
          [-87.80954299952616,
            32.73304399955072
          ],
          [-87.811262999992,
            32.73328600034727
          ],
          [-87.81314899974436,
            32.733550000186405
          ],
          [-87.81556199948278,
            32.733841000379115
          ],
          [-87.81674400040832,
            32.73398300025539
          ],
          [-87.81734499995684,
            32.733966000148676
          ],
          [-87.81794600006495,
            32.733950000141256
          ],
          [-87.81830499996295,
            32.73393899986803
          ],
    /* snip so fit in code snippet */      
          [-87.75765499970949,
            32.65400400025389
          ],
          [-87.75583599967838,
            32.65541199985393
          ],
          [-87.7544120002153,
            32.65708900037058
          ],
          [-87.75301199956941,
            32.65880400035218
          ],
          [-87.75280900048948,
            32.6590519999652
          ],
          [-87.75231900015659,
            32.659396000326346
          ],
          [-87.75213500038586,
            32.65941899972055
          ],
          [-87.75155899945217,
            32.65949200023593
          ],
          [-87.75054099975158,
            32.65943900038774
          ],
          [-87.75001000042319,
            32.65941400028267
          ],
          [-87.74910700049105,
            32.658797000434056
          ],
          [-87.74789000049833,
            32.65810099974256
          ],
          [-87.74615099990197,
            32.65713600032511
          ],
          [-87.74483299982127,
            32.65594599984789
          ],
          [-87.7437830001953,
            32.65435200046195
          ],
          [-87.74317400013099,
            32.65243699999335
          ],
          [-87.74315799994416,
            32.651742999920934
          ],
          [-87.74416099965342,
            32.64773299985494
          ],
          [-87.7445779998169,
            32.64677199988005
          ],
          [-87.74467599964328,
            32.64654599963073
          ],
          [-87.74477500034962,
            32.646319000333456
          ],
          [-87.74480899947017,
            32.64624200003082
          ],
          [-87.74490900023767,
            32.64601100030698
          ],
          [-87.74500900042378,
            32.645780000409054
          ],
          [-87.74538699994211,
            32.64491000043908
          ],
          [-87.7456525992338,
            32.644488905342534
          ],
          [-87.74608900038015,
            32.64379700016904
          ],
          [-87.74625499966338,
            32.64354100016055
          ],
          [-87.7465189995133,
            32.64313399986706
          ],
          [-87.74678199986698,
            32.64272600007504
          ],
          [-87.74694399993967,
            32.64247600007219
          ],
          [-87.74723800030466,
            32.64200600015605
          ],
          [-87.74798200028742,
            32.64081699993649
          ],
          [-87.74869300040613,
            32.63963200010213
          ],
          [-87.74931799984174,
            32.63870500038779
          ],
          [-87.74947999945546,
            32.63841000024515
          ],
          [-87.74971500028688,
            32.63798500015512
          ],
          [-87.74995000041157,
            32.63755899958572
          ],
          [-87.74998000025501,
            32.637504000364736
          ],
          [-87.75000999981965,
            32.6374480003805
          ],
          [-87.75059400050685,
            32.63670800005353
          ],
          [-87.75117899981103,
            32.63596700018743
          ],
          [-87.75205699980991,
            32.63485499961568
          ],
          [-87.7529350000355,
            32.63374300016463
          ],
          [-87.75306099992152,
            32.633583000034676
          ],
          [-87.75373299957329,
            32.6333409998383
          ],
          [-87.75471300037613,
            32.63336199996845
          ],
          [-87.75564900049507,
            32.63369799980667
          ],
          [-87.76290300049492,
            32.636395000189125
          ],
          [-87.76569300044267,
            32.63813699959848
          ],
          [-87.76923299992853,
            32.63982299978322
          ],
          [-87.77380299966116,
            32.64170499963842
          ],
          [-87.77490799965494,
            32.64179600016005
          ],
          [-87.77630400037611,
            32.64159499980391
          ],
          [-87.77818400022585,
            32.64093500011096
          ],
          [-87.77837100011087,
            32.640868999582004
          ],
          [-87.77855900015739,
            32.64080300021816
          ],
          [-87.77867800038428,
            32.64075999984429
          ],
          [-87.77879799960249,
            32.640717999624265
          ],
          [-87.77937599963221,
            32.640514999962186
          ],
          [-87.77995400034239,
            32.64031300005776
          ],
          [-87.78029399953311,
            32.6401940003963
          ],
          [-87.78063400019411,
            32.6400740000741
          ],
          [-87.78163100024317,
            32.63972399992519
          ],
          [-87.78199499988308,
            32.63959599982514
          ],
          [-87.78262799954938,
            32.63937299971171
          ],
          [-87.78283700008154,
            32.63930100019114
          ],
          [-87.78324800017607,
            32.63920399964375
          ],
          [-87.7833970004723,
            32.63916900005259
          ],
          [-87.78354399945756,
            32.63913400007181
          ],
          [-87.78484199949368,
            32.63882700037365
          ],
          [-87.78613900030146,
            32.63852000039503
          ],
          [-87.78746400020542,
            32.638206999586366
          ],
          [-87.78960100012813,
            32.63785199994343
          ],
          [-87.78964800031532,
            32.63784400028408
          ],
          [-87.79024399985028,
            32.637744999582296
          ],
          [-87.79083800034319,
            32.63764700022929
          ],
          [-87.79085099972016,
            32.63764500001907
          ],
          [-87.79104100046837,
            32.63761300013881
          ],
          [-87.7912289997037,
            32.63758199966167
          ],
          [-87.79124499987618,
            32.63757900008125
          ],
          [-87.79152199987207,
            32.63753199992301
          ],
          [-87.7917940004031,
            32.63748700017701
          ],
          [-87.79188999987261,
            32.637470999876086
          ],
          [-87.79255200028018,
            32.63736100019585
          ],
          [-87.79304400030485,
            32.63737200021028
          ],
          [-87.79305799971046,
            32.63737199976539
          ],
          [-87.79399399974783,
            32.63739600002293
          ],
          [-87.79492999961083,
            32.63741999971425
          ],
          [-87.79496499987839,
            32.63742100018533
          ],
          [-87.7972989998378,
            32.63747800001555
          ],
          [-87.80067099979337,
            32.637825000125105
          ],
          [-87.80348999946524,
            32.63776000006337
          ],
          [-87.80363000020596,
            32.63775699994694
          ],
          [-87.8037710003946,
            32.63775399994533
          ],
          [-87.8043890002605,
            32.63773999972145
          ],
          [-87.8077680000098,
            32.63798200036608
          ],
          [-87.80942899989908,
            32.63844599976793
          ],
          [-87.81011800038851,
            32.63817599980049
          ],
          [-87.81021399971384,
            32.63813700015482
          ],
          [-87.81029300032085,
            32.63805299968207
          ],
          [-87.81053199989321,
            32.63779399986768
          ],
          [-87.81077199949964,
            32.63753499982235
          ],
          [-87.81119200027973,
            32.63708199970983
          ],
          [-87.81161200034192,
            32.63663100026363
          ],
          [-87.81164299946546,
            32.63659800022752
          ],
          [-87.81182600011093,
            32.636308000061916
          ],
          [-87.81227600040745,
            32.63561300036989
          ],
          [-87.81272600012582,
            32.63491800044168
          ],
          [-87.81340100012703,
            32.63387499964957
          ],
          [-87.81337099989322,
            32.63303500000622
          ],
          [-87.81332699989366,
            32.63292499956406
          ],
          [-87.8132659996678,
            32.63277300036689
          ],
          [-87.81320499971031,
            32.63262000041888
          ],
          [-87.81274199961568,
            32.63146199974547
          ],
          [-87.81200199995088,
            32.630436000368604
          ],
          [-87.81083400019473,
            32.62881599970831
          ],
          [-87.80891899953525,
            32.62710499991795
          ],
          [-87.80826699970142,
            32.626015999917016
          ],
          [-87.80802799957688,
            32.62513300028531
          ],
          [-87.80775200009656,
            32.622312999984
          ],
          [-87.80750499973502,
            32.62129400035338
          ],
          [-87.80735699974467,
            32.62068300019218
          ],
          [-87.80675300014678,
            32.61928700024607
          ],
          [-87.80442900049195,
            32.6164839996128
          ],
          [-87.80143800046204,
            32.613432999647166
          ],
          [-87.79843599957003,
            32.6104469998922
          ],
          [-87.79704900050626,
            32.60961299981022
          ],
          [-87.7964320004202,
            32.60924300022081
          ],
          [-87.79572400003444,
            32.60891900013077
          ],
          [-87.79495999944558,
            32.608569000307185
          ],
          [-87.79419300038279,
            32.60821799991501
          ],
          [-87.79417000013103,
            32.6082069996828
          ],
          [-87.794032999462,
            32.608164000407605
          ],
          [-87.79259799997406,
            32.60770399977989
          ],
          [-87.79114199996442,
            32.607240999826836
          ],
          [-87.79093100047821,
            32.60716899991042
          ],
          [-87.7907259999825,
            32.60710299957984
          ],
          [-87.78950700035247,
            32.60671300006783
          ],
          [-87.78878100008956,
            32.606444000381856
          ],
          [-87.78822699964478,
            32.60623899958428
          ],
          [-87.78767099973224,
            32.606031999653574
          ],
          [-87.78762099982663,
            32.606014999842806
          ],
          [-87.78724999962238,
            32.60587699995636
          ],
          [-87.78687800039725,
            32.605739000100165
          ],
          [-87.78676600041744,
            32.60569700020314
          ],
          [-87.7866530000644,
            32.605656000401865
          ],
          [-87.78561499959663,
            32.60527199989833
          ],
          [-87.78456599981854,
            32.605049000239575
          ],
          [-87.783930999768,
            32.60513900000557
          ],
          [-87.78368999976696,
            32.60517300004584
          ],
          [-87.78344999999356,
            32.6052060002845
          ],
          [-87.78327100042944,
            32.60523099975866
          ],
          [-87.78303899995325,
            32.60534800021888
          ],
          [-87.7823800004109,
            32.60568200005899
          ],
          [-87.78171900025124,
            32.6060180002615
          ],
          [-87.7807770002937,
            32.60649900042521
          ],
          [-87.77678799948126,
            32.607988999883524
          ],
          [-87.77380700002077,
            32.60876500002242
          ],
          [-87.77367300009604,
            32.60878300023979
          ],
          [-87.77109199961123,
            32.60913599957639
          ],
          [-87.768814999614,
            32.60963699988253
          ],
          [-87.767312999701,
            32.61051999974514
          ],
          [-87.76700700053549,
            32.61069999994026
          ],
          [-87.76507999993886,
            32.611721999826344
          ],
          [-87.7635230004629,
            32.61254400038951
          ],
          [-87.76259400007433,
            32.61284099997004
          ],
          [-87.76241999975562,
            32.612895999746684
          ],
          [-87.7601510002289,
            32.613619000238515
          ],
          [-87.7599759998112,
            32.6136540002568
          ],
          [-87.75673000053195,
            32.614308999841626
          ],
          [-87.75661299946415,
            32.61432500017049
          ],
          [-87.75622399998312,
            32.61434800017994
          ],
          [-87.75582399985906,
            32.61436799976028
          ],
          [-87.75501599953778,
            32.614409000080705
          ],
          [-87.75412900017625,
            32.614453999769154
          ],
          [-87.75259700007595,
            32.61430799964524
          ],
          [-87.75215499969215,
            32.614265999634
          ],
          [-87.75179399986875,
            32.61421000022443
          ],
          [-87.75157399966103,
            32.61417599999126
          ],
          [-87.75124300051276,
            32.614126999627025
          ],
          [-87.75091399975204,
            32.614076999903965
          ],
          [-87.75069400044146,
            32.61404399964045
          ],
          [-87.75055799953847,
            32.614024000073826
          ],
          [-87.7503520000532,
            32.613993000386074
          ],
          [-87.75014699964872,
            32.61396100029381
          ],
          [-87.7500099994434,
            32.613940000307046
          ],
          [-87.74993299953691,
            32.61388100041255
          ],
          [-87.74947399991392,
            32.61352599955067
          ],
          [-87.74914400032429,
            32.613292999798915
          ],
          [-87.74900799990189,
            32.61319599991977
          ],
          [-87.74872299995617,
            32.61281599959827
          ],
          [-87.74871599979572,
            32.61275499961563
          ],
          [-87.748685000374,
            32.61249300035157
          ],
          [-87.7488380000892,
            32.61190200010822
          ],
          [-87.74880100020705,
            32.61118500039227
          ],
          [-87.74866600003872,
            32.61078000030628
          ],
          [-87.74850800054772,
            32.61030399989787
          ],
          [-87.74817300008237,
            32.609087999725986
          ],
          [-87.74732300026649,
            32.60712900002935
          ],
          [-87.74702900000284,
            32.60647800018721
          ],
          [-87.74691400050799,
            32.60622300014419
          ],
          [-87.7467990001023,
            32.60597000043332
          ],
          [-87.74656600030124,
            32.605453999703556
          ],
          [-87.74633300032662,
            32.604939000419904
          ],
          [-87.74625999951398,
            32.604778000197655
          ],
          [-87.74597499987506,
            32.604111999730236
          ],
          [-87.74591199997724,
            32.60396599975714
          ],
          [-87.74584900003303,
            32.6038189998028
          ],
          [-87.74553999991784,
            32.60309799972407
          ],
          [-87.74545399986363,
            32.60288800032072
          ],
          [-87.74470500003746,
            32.601070000091404
          ],
          [-87.74418499991806,
            32.59910599956559
          ],
          [-87.74401799981898,
            32.59868899970994
          ],
          [-87.74359700000814,
            32.597636999974206
          ],
          [-87.74344300007687,
            32.597437000371556
          ],
          [-87.74338799981639,
            32.597367000147194
          ],
          [-87.74333500011866,
            32.597297999817954
          ],
          [-87.74319000036711,
            32.59711200010883
          ],
          [-87.7430480002025,
            32.596928999790016
          ],
    
          // snip so fits in code snippet
          [-87.71570900046997,
            33.00682400017906
          ]
        ]
      ]
    };
    /* Always set the map height explicitly to define the size of the div
           * element that contains the map. */
    
    #map {
      height: 100%;
    }
    
    
    /* Optional: Makes the sample page fill the window. */
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <title>Simple Polygon</title>
      <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
      <!-- jsFiddle will insert css and js -->
    </head>
    
    <body>
      <div id="map"></div>
      <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
      <script src="https://cdn.jsdelivr.net/gh/openplacedatabase/gmaps-large-polygons@master/gmaps-large-polygon.js"></script>
      <script src='https://rawgithub.com/openplacedatabase/geojson-google-maps/master/geojson-google-maps.js'></script>
    </body>
    
    </html>

    【讨论】:

      猜你喜欢
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      相关资源
      最近更新 更多