Create boundary area in Google Maps using coordinates - javascript
I want to show an area boundary in Google maps using API v3. I've got a list of postcodes for the area that the client covers, I've converted these to LatLng coordinates and can create a polygon with them but as you would expect, it's a mess of lines and not a solid shape area.
Anyone know if this is possible and if so, how to do this? Or a different way of doing this. Code I'm using below:
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 55.921772, lng: -3.383983},
scrollwheel: false,
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: true
});
// Define the LatLng coordinates for the polygon's path.
var triangleCoords = [
new google.maps.LatLng(55.948969,-3.1927988),
new google.maps.LatLng(55.949292,-3.209399),
new google.maps.LatLng(55.957032,-3.1850223),
new google.maps.LatLng(55.929196,-3.2089489),
new google.maps.LatLng(55.934519,-3.2131166),
new google.maps.LatLng(55.924534,-3.2096679),
new google.maps.LatLng(55.901564,-3.2035307),
new google.maps.LatLng(55.940591,-3.2170048),
new google.maps.LatLng(55.943847,-3.2184679),
new google.maps.LatLng(55.932823,-3.2462462),
new google.maps.LatLng(55.924084,-3.2938003),
new google.maps.LatLng(55.948636,-3.3239403),
new google.maps.LatLng(55.94727,-3.2158532),
new google.maps.LatLng(55.946459,-3.2359557),
new google.maps.LatLng(55.942119,-3.2790791),
new google.maps.LatLng(55.943678,-3.2820926),
new google.maps.LatLng(55.933458,-3.2867013),
new google.maps.LatLng(55.907231,-3.2498242),
new google.maps.LatLng(55.914285,-3.2391396),
new google.maps.LatLng(55.929872,-3.2483283),
new google.maps.LatLng(55.919613,-3.272673),
new google.maps.LatLng(55.913635,-3.2773089),
new google.maps.LatLng(55.953223,-3.1155757),
new google.maps.LatLng(55.951535,-3.1124523),
new google.maps.LatLng(55.944854,-3.1050555),
new google.maps.LatLng(55.931274,-3.1456767),
new google.maps.LatLng(55.938277,-3.1758846),
new google.maps.LatLng(55.919988,-3.1677619),
new google.maps.LatLng(55.907921,-3.1339982),
new google.maps.LatLng(55.899518,-3.1649876),
new google.maps.LatLng(55.955011,-3.1932569),
new google.maps.LatLng(55.953699,-3.1905739),
new google.maps.LatLng(55.951683,-3.2010498),
new google.maps.LatLng(55.951121,-3.2034185),
new google.maps.LatLng(55.877027,-3.1487295),
new google.maps.LatLng(55.943146,-3.0559971),
new google.maps.LatLng(55.943718,-3.047895),
new google.maps.LatLng(55.939247,-3.0130501),
new google.maps.LatLng(55.892996,-3.0728633),
new google.maps.LatLng(55.89186,-3.0692661),
new google.maps.LatLng(55.892996,-3.0728633),
new google.maps.LatLng(55.956248,-3.4049741),
new google.maps.LatLng(55.958401,-3.204173),
new google.maps.LatLng(55.953391,-3.2083257),
new google.maps.LatLng(55.947542,-3.2146128),
new google.maps.LatLng(55.943346,-3.2109749),
new google.maps.LatLng(55.945203,-3.2049162),
new google.maps.LatLng(55.973614,-3.3522429),
new google.maps.LatLng(55.959293,-2.9831886),
new google.maps.LatLng(55.958761,-3.2259363),
new google.maps.LatLng(55.958258,-3.2509238),
new google.maps.LatLng(55.954508,-3.2162083),
new google.maps.LatLng(55.969754,-3.2567425),
new google.maps.LatLng(55.965345,-3.2713865),
new google.maps.LatLng(55.966799,-3.2759201),
new google.maps.LatLng(55.961259,-3.2624101),
new google.maps.LatLng(55.960523,-3.3189479),
new google.maps.LatLng(55.980282,-3.2222259),
new google.maps.LatLng(55.970837,-3.2150703),
new google.maps.LatLng(55.972055,-3.1971305),
new google.maps.LatLng(55.977169,-3.1812151),
new google.maps.LatLng(55.970188,-3.1726547),
new google.maps.LatLng(55.970989,-3.1715573),
new google.maps.LatLng(55.9706,-3.1709047),
new google.maps.LatLng(55.969318,-3.1629671),
new google.maps.LatLng(55.958457,-3.1835442),
new google.maps.LatLng(55.954315,-3.1853397),
new google.maps.LatLng(55.956023,-3.1607265),
new google.maps.LatLng(55.955162,-3.15005),
new google.maps.LatLng(55.950521,-3.1836543),
new google.maps.LatLng(55.94698,-3.1866047),
new google.maps.LatLng(55.934593,-3.1935895),
new google.maps.LatLng(55.934464,-3.1948181),
new google.maps.LatLng(55.930516,-3.1756174),
new google.maps.LatLng(55.97862,-3.2535203),
new google.maps.LatLng(55.951962,-3.1749222)
];
// Construct the polygon.
var areaCovered = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.25
});
areaCovered.setMap(map);
}
</script>
Okay here is that code example I promised. This will give you a square/rectangle polygon that contains all the points in triangleCoords.
// create new LatLngBounds
var bounds = new google.maps.LatLngBounds();
// extend bounds to contain each coordinate
for (var i = 0, i_end = triangleCoords.length; i < i_end; i++) {
bounds.extend(triangleCoords[i]);
}
// get the NSEW corners of the bounds
var NE = bounds.getNorthEast();
var SW = bounds.getSouthWest();
var SE = { lat: SW.lat(), lng: NE.lng() };
var NW = { lat: NE.lat(), lng: SW.lng() };
// Construct the polygon.
var areaCovered = new google.maps.Polygon({
paths: [NE, SE, SW, NW],
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.25
});
areaCovered.setMap(map);
I also wanted to clarify why your code was creating a mess of lines. A polygon will construct its boundary based on the order of the coordinates you supply it. To get an idea of what I'm talking about you can change the order in my example to this paths: [NE, SW, SE, NW].
To give you a visual, you can think of it like the game of connect the dots. If you didn't connect these dots in order you would end up with a mess of lines instead of a cute laughing Pikachu.
My mistake, the Polygon have NOT to be closed.
Bevor Edit:
Your Polygon have to be closed.
As in https://developers.google.com/maps/documentation/javascript/shapes , where i think you get your source from, the last and the first point are the same.
Related
How to draw a line and a box on gmaps?
I've seen this tool which let you draw a line on gmaps and it generates the js code for you So the JS is: var myCoordinates = [ new google.maps.LatLng(48.955410,10.034749), new google.maps.LatLng(59.648652,29.898030) ]; var polyOptions = { path: myCoordinates, strokeColor: "#FF0000", strokeOpacity: 1, strokeWeight: 3 } var it = new google.maps.Polyline(polyOptions); it.setMap(map); What I would like to do is to start the line from a pin I receive and not a pin I set when I click as per that tool and then I would to draw a infobox at the end of that line (so not where it starts form the pin). What I am aiming for is to draw a line form a starting point and have an infobox such as per this image below, see the lines on the map Therefore I can pass the coords here: new google.maps.LatLng(48.955410,10.034749), new google.maps.LatLng(59.648652,29.898030) But how would I target the end of the line and place text there? With this answer I can define a start and end, but how to draw a box at the end point?
I think you can do it using a marker at the end point of the line, then attaching, for example an infoWindow, at the end and finally hiding the marker. function initMap() { var coordinates = { lat: 40.785845, lng: -74.020496 }; var coordinates2 = { lat: 40.805845, lng: -74.130496 }; var map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: coordinates, scrollwheel: false }); var marker = new google.maps.Marker({ position: coordinates, map: map }); var infoMarker = new google.maps.Marker({ position: coordinates2, map: map }); var infowWindow = new google.maps.InfoWindow(); var line = new google.maps.Polyline({ path: [ marker.position, infoMarker.position ], strokeColor: "#FF0000", strokeOpacity: 1, strokeWeight: 3 }); line.setMap(map); infowWindow.setContent("<b>Hello world!</b>"); infowWindow.open(map, infoMarker); infoMarker.setVisible(false); } google.maps.event.addDomListener(window, "load", initMap); Check it working on this jsfiddle
how to get an image to point in a certain direction in a visualisation map
So I am using JavaScript to plot a position of a flight and its flight path on a visualisation map. I am getting the data from a node server that I have created. I have looked at similar question on stack over flow but I haven't been able to get them working. I am currently struggling with the positioning of the flight icon, as I want it to point in the same direction as the flight path or even put a marker at the top of the image so it can point to a certain latitude and longitude. At the minute the flight icon only faces north. Here is a screenshot of my code. I tried using the anchor in the Google maps API but I couldn't get it working so I commented it out. var flightCord = []; for (var i = 0; i < data.flight.length; i++) { var lat = data.flight[0].lat1; var lng = data.flight[0].lng1; // Co-ordinates for Dublin Airport 53.421379, -6.27 var image = new google.maps.MarkerImage("plane3.png" //new google.maps.Size(25,25), //null, null, //new google.maps.Point(53.4213879,-6.27) //new google.maps.Point(0, 50) ); var latLng = new google.maps.LatLng(lat,lng); var marker = new google.maps.Marker({ position: latLng, map: map, icon: image }); var flightCord = [ {lat:data.flight[0].lat1, lng: data.flight[0].lng1}, {lat:data.flight[i].lat1, lng:data.flight[i].lng1}]; } var flightPath = new google.maps.Polyline({ path: flightCord, geodesic: true, strokeColor: '#FF0000', strokeOpacity: 1.0, strokeWeight: 1 });
Google Maps API circle markers, variable size
var xml = data.responseXML; var circles = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var name = markers[i].getAttribute("location"); var scans = markers[i].getAttribute("scans"); var point = new google.maps.LatLng( parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("long"))); var html = name; var marker = new google.maps.Marker({ center: center, strokeColor: '#FF0000', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#FF0000', fillOpacity: 0.35, map: map, radius:1000 }); bindInfoWindow(marker, map, infoWindow, html); } Trying to add some data to circle markers. The circles are already markers for specific locations, but I want to make them vary in size depending on a count that corresponds to that location. I cannot seem to find any code to make markers of variable size, since each marker is most likely going to have a unique number of contacts. Any ideas? Here's the code I have now for markers. I know its not right, since it's not producing what I want. https://developers.google.com/maps/documentation/javascript/examples/circle-simple <---This is the effect that I am looking for, but I don't understand how to get the values for size to change based on the data entered in the table.
You can set the marker radius to an integer returned by a function. Or it can be an expression. Here is a JsFiddle with a working example where the count attribute in the XML is set by an expression. function initMap() { var myLatLng = new google.maps.LatLng(47.6685771, -122.2553681), myOptions = { zoom: 12, center: myLatLng, mapTypeId: google.maps.MapTypeId.ROADMAP }, map = new google.maps.Map(document.getElementById('map'), myOptions); var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; ++i) { var marker = markers[i]; var name = marker.getAttribute("location"); var point = new google.maps.LatLng( parseFloat(marker.getAttribute("lat")), parseFloat(marker.getAttribute("long"))); var cityCircle = new google.maps.Circle({ strokeColor: '#FF0000', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#FF0000', fillOpacity: 0.35, map: map, center: point, radius: marker.getAttribute("count") * 75 }); } } https://jsfiddle.net/plbogen/ecj8o4uL
Google Maps API v3 Polygon closing
When creating a polygon using the Google Maps API v3, how can I prevent the polygon from snapping to other 'maps'? So instead of: I would like to make the polygon close like this:
The issue occurs when the difference between the longitudes of 2 consecutive points in the path is >=180 the longitudes of the first 2 points are -97 and 93, so that's the problem in this case(difference is 190) The only thing I may suggest so far is to split this portion of the path: new google.maps.LatLng(81, -97), //additional point new google.maps.LatLng(80.5, -12), new google.maps.LatLng(80, 93), new google.maps.LatLng(56, 78), new google.maps.LatLng(60, -94) Demo: http://jsfiddle.net/doktormolle/39CtV/
Once I also Needed it , and thankfully Devs had provided there Research ,so i'm sharing With you //Taking Example of Bermuda Triangle function initialize() { var mapOptions = { zoom: 5, center: new google.maps.LatLng(24.886436490787712, -70.2685546875), mapTypeId: google.maps.MapTypeId.TERRAIN }; var bermudaTriangle; var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); // Define the LatLng coordinates for the polygon's path. var triangleCoords = [ new google.maps.LatLng(25.774252, -80.190262), new google.maps.LatLng(18.466465, -66.118292), new google.maps.LatLng(32.321384, -64.75737), new google.maps.LatLng(25.774252, -80.190262) ]; // Construct the polygon. bermudaTriangle = new google.maps.Polygon({ paths: triangleCoords, strokeColor: '#FF0000', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#FF0000', fillOpacity: 0.35 }); bermudaTriangle.setMap(map); } google.maps.event.addDomListener(window, 'load', initialize); For More Reference https://developers.google.com/maps/documentation/javascript/examples/polygon-simple
Google maps - add road directions, circle, postal code areas
I got a basic map running using google maps v3 Next features that I would like to add include: draw line as road directions instead of direct point to point polyline draw a circle 75 km circle around from a specific point highlight the postal code of a specific point. I Would appreciate people's thoughts on these topics var geocoder; var map; function fnPresentMap() { geocoder = new google.maps.Geocoder(); var locationArray = new Array(); locationArray[0] = new Array(); locationArray[1] = new Array(); locationArray[0][0] = document.getElementById('LAT_OUT_1').innerHTML; locationArray[0][1] = document.getElementById('LON_OUT_1').innerHTML; locationArray[1][0] = document.getElementById('LAT_OUT_2').innerHTML; locationArray[1][1] = document.getElementById('LON_OUT_2').innerHTML; var latlng = new google.maps.LatLng(44, -75); var myOptions = { zoom: 8, center: latlng, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, navigationControl: true, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map"), myOptions); var myLatlng; var image_name; for (var count = 0; count < locationArray.length; ++count){ image_name = "img/marker_"+(count+1)+".png"; myLatlng = new google.maps.LatLng(locationArray[count][0],locationArray[count][1]); var marker = new google.maps.Marker({ position: myLatlng, map: map, icon: image_name }); } // re-center var centerLocation = new google.maps.LatLng(locationArray[0][0],locationArray[0][1]); map.setCenter(centerLocation); // show line var points = [ new google.maps.LatLng(locationArray[0][0],locationArray[0][1]), new google.maps.LatLng(locationArray[1][0],locationArray[1][1]) ]; var line = new google.maps.Polyline({ map: map, path: points, strokeColor: "#FF0000", strokeWeight: 2, strokeOpacity: 1.0 }); }
To render road directions between two specific points, you need to use the Google Maps API directions service. If you check out the documentation you will find pretty straightforward examples to make a directions request and render the results on a map as a line between the two points. I think the best approach for drawing a circle around a point is to draw a polygon with enough points to approximate a circle. You can find a good example of this here. To highlight the postcode at a specific point, I suggest you use the Google Maps API reverse geocoding service (convert from a latitude/longitude to a human readable address). You can extract the postcode from the JSON response you get back and then display it on the map using a infoWindow or some other kind of overlay.