Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps how to Show city or an Area outline

How to show the places or Area outline just like the below sites does: [I enter an area and it shows the clear border for that area.]

enter image description here

Link

I have searched for hours in the official maps docs and there was no clue how these website are showing the area borders. Also looked at some old questions and read its not possible then.

like image 554
Chanakya Vadla Avatar asked Aug 06 '15 17:08

Chanakya Vadla


People also ask

How do you highlight an area city state or country on Google Maps?

To achieve this, you will basically need to look for the coordinates of the state or country on GADM. Once you have them, draw a shape on the Google Maps with the Polygon object providing an array with all the coordinates that play basically as borders of the place that you want to highlight.

Can I draw an area on Google Maps?

You can trace a path or highlight an area on your map by drawing lines and shapes.


2 Answers

From what I searched, at this moment there is no option from Google in the Maps API v3 and there is an issue on the Google Maps API going back to 2008. There are some older questions - Add "Search Area" outline onto google maps result , Google has started highlighting search areas in Pink color. Is this feature available in Google Maps API 3? and you might find some newer answers here with updated information, but this is not a feature.

What you can do is draw shapes on your map - but for this you need to have the coordinates of the borders of your region.

Now, in order to get the administrative area boundaries, you will have to do a little work: http://www.gadm.org/country (if you are lucky and there is enough level of detail available there).

On this website you can locally download a file (there are many formats available) with the .kmz extension. Unzip it and you will have a .kml file which contains most administrative areas (cities, villages).

  <?xml version="1.0" encoding="utf-8" ?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document id="root_doc"> <Schema name="x" id="x">     <SimpleField name="ID_0" type="int"></SimpleField>     <SimpleField name="ISO" type="string"></SimpleField>     <SimpleField name="NAME_0" type="string"></SimpleField>     <SimpleField name="ID_1" type="string"></SimpleField>     <SimpleField name="NAME_1" type="string"></SimpleField>     <SimpleField name="ID_2" type="string"></SimpleField>     <SimpleField name="NAME_2" type="string"></SimpleField>     <SimpleField name="TYPE_2" type="string"></SimpleField>     <SimpleField name="ENGTYPE_2" type="string"></SimpleField>     <SimpleField name="NL_NAME_2" type="string"></SimpleField>     <SimpleField name="VARNAME_2" type="string"></SimpleField>     <SimpleField name="Shape_Length" type="float"></SimpleField>     <SimpleField name="Shape_Area" type="float"></SimpleField> </Schema> <Folder><name>x</name>   <Placemark>     <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>     <ExtendedData><SchemaData schemaUrl="#x">         <SimpleData name="ID_0">186</SimpleData>         <SimpleData name="ISO">ROU</SimpleData>         <SimpleData name="NAME_0">Romania</SimpleData>         <SimpleData name="ID_1">1</SimpleData>         <SimpleData name="NAME_1">Alba</SimpleData>         <SimpleData name="ID_2">1</SimpleData>         <SimpleData name="NAME_2">Abrud</SimpleData>         <SimpleData name="TYPE_2">Comune</SimpleData>         <SimpleData name="ENGTYPE_2">Commune</SimpleData>         <SimpleData name="VARNAME_2">Oras Abrud</SimpleData>         <SimpleData name="Shape_Length">0.2792904164402</SimpleData>         <SimpleData name="Shape_Area">0.00302673357146115</SimpleData>     </SchemaData></ExtendedData>       <MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>23.117561340332031,46.269237518310547 23.108898162841797,46.265365600585937 23.107486724853629,46.264305114746207 23.104681015014762,46.260105133056641 23.101633071899471,46.250000000000114 23.100803375244254,46.249053955078239 23.097520828247184,46.246582031250114 23.0965576171875,46.245487213134822 23.095674514770508,46.244930267334098 23.092174530029354,46.243438720703182 23.088010787963924,46.240383148193473 23.083366394043082,46.238204956054801 23.075212478637809,46.234935760498047 23.071325302123967,46.239696502685547 23.070602416992131,46.241668701171875 23.069700241088924,46.242824554443416 23.068435668945369,46.243541717529354 23.066627502441406,46.244037628173771 23.064964294433651,46.246234893798885 23.062850952148437,46.247486114501953 23.0626220703125,46.248153686523438 23.062761306762752,46.250873565673942 23.061862945556697,46.255172729492301 23.061449050903434,46.256267547607422 23.05998420715332,46.258060455322322 23.057676315307674,46.259838104248161 23.055141448974666,46.262714385986442 23.053401947021484,46.264244079589901 23.049621582031193,46.266674041748161 23.043565750122013,46.268516540527457 23.041521072387695,46.269458770751953 23.034791946411076,46.270542144775334 23.027051925659293,46.27105712890625 23.025453567504826,46.271255493164063 23.022710800170898,46.272083282470703 23.020351409912053,46.271331787109432 23.018688201904297,46.270687103271598 23.015596389770508,46.270793914794922 23.014116287231502,46.271579742431697 23.009817123413143,46.275333404541016 23.006668090820426,46.277061462402401 23.004106521606445,46.279254913330135 23.001775741577205,46.282882690429688 23.005559921264648,46.283077239990348 23.009967803955135,46.28415679931652 23.014947891235465,46.286224365234489 23.019996643066463,46.28900146484375 23.024263381958121,46.292709350586051 23.027633666992301,46.295299530029411 23.028041839599609,46.295692443847656 23.032444000244197,46.294342041015625 23.03491401672369,46.293315887451229 23.044847488403434,46.290401458740234 23.047790527343807,46.28928375244152 23.053009033203239,46.288627624511719 23.057231903076229,46.288341522216797 23.064565658569393,46.287548065185547 23.070388793945426,46.286254882812614 23.075139999389592,46.284847259521428 23.075983047485465,46.284801483154411 23.085800170898494,46.28253173828125 23.098115921020451,46.280982971191406 23.099718093872127,46.280590057373104 23.105833053588981,46.278388977050838 23.112155914306641,46.274082183837947 23.116207122802791,46.270610809326172 23.117561340332031,46.269237518310547</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>   </Placemark> </Folder> </Document></kml> 

From this point on, when the user searches for a city/village, you simply retrieve the boundaries and draw around those coordinates on the map - https://developers.google.com/maps/documentation/javascript/overlays#Polygons

I hope this helps you! Good luck!

border on Google Map using the above coordinates UPDATE: I made the borders of this city using the coordinates above

                   var ctaLayer = new google.maps.KmlLayer({                        url: 'https://www.dropbox.com/s/0grhlim3q4572jp/ROU_adm2%20-%20Copy.kml?dl=1'   });   ctaLayer.setMap(map); 

(I put a small kml file on my Dropbox containing the borders of a single city)

Note that this uses the Google built in KML system, in which it their server gets the file, computes the view and spits it back to you - it has limited usage and I used it to show you how the borders look. In your application you should be able to parse the coordinates from the kml file, put them in an array (as the polygon documentation tells you - https://developers.google.com/maps/documentation/javascript/examples/polygon-arrays ) and display them.

Note that there will be differences between the borders that Google sets on http://www.google.com/maps and the borders that you will get with this data.

Good luck! enter image description here

UPDATE: http://pastebin.com/x2V1aarJ , http://pastebin.com/Gh55EDW5 These are the javascript files (they were minified, so I used an online tool to make them readable) from the website. If you are not fully satisfied with this my solution, feel free to study them.

Best of luck!

like image 119
radu-matei Avatar answered Sep 28 '22 19:09

radu-matei


i was looking for the same and found the answer,

solution is to use the styled map, on below link you can create your custom styles through wizard and test is at the same time google map style wizard

you can check all available options : here

here is my sample code which creates boundary for states and hide all the road and there labels.

    var styles = [   {     "featureType": "administrative.province",     "elementType": "geometry.stroke",     "stylers": [       { "visibility": "on" },       { "weight": 2.5 },       { "color": "#24b0e2" }     ]   },{     "featureType": "road",     "elementType": "geometry",     "stylers": [       { "visibility": "off" }     ]   },{     "featureType": "administrative.locality",     "stylers": [       { "visibility": "off" }     ]   },{     "featureType": "road",     "elementType": "labels",     "stylers": [       { "visibility": "off" }     ]   } ];    var  geocoder = new google.maps.Geocoder();    geocoder.geocode({        'address': "rajasthan"    }, (results, status)=> {      var mapOpts = {        mapTypeId: google.maps.MapTypeId.ROADMAP,        scaleControl: true,        scrollwheel: false,        styles:styles,        center: results[0].geometry.location,        zoom:6      }      map = new google.maps.Map(document.getElementById("map"), mapOpts);    }); 
like image 41
abhirathore2006 Avatar answered Sep 28 '22 21:09

abhirathore2006