I'm trying to create a map with a lot of flight paths, gathered from a database.
The method i use could probably be improved a little:
var flightPlanCoordinates1 = [
new google.maps.LatLng(53.63384159955519, 10.005816800985485),
new google.maps.LatLng(40.689837457540044, -74.17809198377654)
];
var flightPath1 = new google.maps.Polyline({
path: flightPlanCoordinates1,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath1.setMap(map);
...
The above will be looped over and over to show all the routes collected from the database.
So my question is, if it would be possible to simplify this, so that all of the above does not have to be looped, only the coordinates.
What i'm thinking is that a "break" function for "flightPlanCoordinates1", to break for each route, would be a good solution.
Appreciate any help
First, define your polyline options:
var pathOptions = {
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
};
Then in your loop you can create the polyline with the options:
var path = new google.maps.Polyline(pathOptions);
Then get the start and end points (where start_lat/lng end_lat/lng are your coordinates):
var start_point = new google.maps.LatLng(start_lat, start_lng);
var end_point = new google.maps.LatLng(end_lat, end_lng);
Then apply it to the polyline and set it on the map:
path.getPath().setAt(0, start_point);
path.getPath().setAt(1, end_point);
path.setMap(map);
You get the idea?
Related
Just a quick query - I'm sure it has a quick answer :)
I'm trying to loop through an array of points defining a polygon in google maps and change them (my test programme is just decrementing the latitude by a small amount, to see if I can get it to work). I've taken my experimental code from the Bermuda Triangle example, but with a LatLong array rather than MVC.
triangleCoords = [
new google.maps.LatLng(25.774, -80.190 ),
new google.maps.LatLng(18.466, -66.118 ),
new google.maps.LatLng(32.321, -64.757 )
];
bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map
});
And I'm trying to alter the points with this:
var vertices = bermudaTriangle.getPath();
for (var i =0; i < vertices.getLength(); i++) {
var xy = vertices.getAt(i);
vertices.setAt(i, new google.maps.LatLng( xy.lat()-0.01, xy.lng() ));
}
But it doesn't work. Can anyone see what is wrong? Thanks
You've got an array of coordinates, saved as the variable vertices, which you've then updated with new values... and then what? All you've done is update an array.
If you want to redraw the polygon, you also need to then do:
bermudaTriangle.setPath(vertices);
I'm building an application using MEAN Stack and Google Maps Api and I'm having a little problem when it comes to draw Polygons and LineStrings.
I have a collection of geometries (Points, Polygons and LineStrings) and each time I find one of them I need to add it properly to the map.
I have no problem when it comes to render Markers but I have issues when it comes to Polygons and LineStrings.
Here's my code which contains the logic of initializing the map.
// Initializes the map
function initialize(latitude, longitude, filter) {
// If map has not been created...
if (!map) {
// Create a new map and place in the index.html page
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: new google.maps.LatLng(vm.selectedLat, vm.selectedLong)
});
}
// Loop through each location in the array and place a geometry
locations.forEach(function (n) {
if(n.type === 'LineString'){
console.log('LineString '+JSON.stringify(n.coords));
var linestring = new google.maps.Polyline({
position: n.coords,
map: map,
geodesic: true,
strokeColor: '#0404B4',
strokeOpacity: 1.0,
strokeWeight: 2
});
// For each linestring created, add a listener
google.maps.event.addListener(linestring, 'click', function () {
// When clicked, open the selected linestring's message
n.message.open(map, linestring);
});
linestring.setMap(map);
}
if(n.type === 'Polygon'){
console.log('Polygon '+JSON.stringify(n.coords));
var polygon = new google.maps.Polygon({
path: n.coords,
geodesic: true,
strokeColor: '#0404B4',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#0404B4',
fillOpacity: 0.35
});
// For each polygon created, add a listener
google.maps.event.addListener(polygon, 'click', function () {
// When clicked, open the selected polygon's message
n.message.open(map, polygon);
});
polygon.setMap(map);
}
});
};
Here are the screenshots of the two console.log() of the coordinates
As you can see from the screenshot I also get
InvalidValueError: at index 0: not a LatLng or LatLngLiteral: in property lat: not a number
I tried, unsuccessfully, to find a solution to that.
Why can't I draw these geometries? How can I solve the InvalidValueError?
Thanks in advance.
Issues:
A google.maps.Polyline doesn't have a position property, it has a path property which takes an array of google.maps.LatLngLiterals (which looks like what you are passing in to the position property).
A google.maps.Polygon has a paths property which takes an array of google.maps.LatLngLiterals, but your data is not formatted correctly (and you are using path not paths). It is an array of {lat: [46.774, -48.19], lng: [46.466, -29.119]}, neither lat nor lng are Numbers, they are arrays.
I have about 2700 markers on the map (in small area) and I'm using OverlappingMarkerSpiderfier and marker cluster.
I tried to add polylines between some of the markers. (I have parent - children relationship).
The problem is that after about 100 polylines, the map became extermaly slow, and sometimes even the tab crashes.
The connection can be static and can be only on visable area.
Do you have any suggestion to alternatives to polylines or other solution ?
Here is the code of adding polylines:
if (currentPoint.parentId !== "0"){
var parentPoint = result.message[parseInt(currentPoint.parentId)];
var coordinates = [
{lat: parseFloat(currentPoint.latitude), lng: parseFloat(currentPoint.longitude)},
{lat: parseFloat(parentPoint.latitude), lng: parseFloat(parentPoint.longitude)}
];
var path = new google.maps.Polyline({
path: coordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
path.setMap(map);
}
p.s: I know that all the parsing operations takes resources, but I can't change the DB scheme.
Thanks in advance.
I'm trying to use the JavaScript API for GoogleMaps to draw a bunch of lines but I keep getting syntax errors I don't understand. My data is stored as such:
var line_map = {};
line_map['l1'] = {
path: [new google.maps.LatLng(42.3581, -71.0636), new google.maps.LatLng(42.351821, -71.045461)],
weight: 2
};
With many other line entries. I then try to use it with the following:
for (var entry in line_map) {
var line = new google.maps.Polyline({
path: entry.path,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: entry.weight
});
// Add the line to map
line.setMap(map);
}
However I keep getting an error that says Invalid value for constructor parameter 0: undefined
I get that it's saying entry.path is undefined but I don't understand why because I clearly defined it in the entry for l1
You should change this:
path: entry.path
to
path: line_map[entry].path
And do the same with the weight. Check out this working fiddle - I changed some coordinates to see the line.
EDIT: There is also a good explanation on this post about the for...in loop and objects.
I am trying to populate an array in my Delphi program with data produced by Javascript.
I have a WebBrowser that has loaded HTML with the following Javascript:
function DrawTrack () {
var geo_path = new Array ();
var geo_path = [
new google.maps.LatLng (-18.142599, 178.431),
new google.maps.LatLng (-27.46758, 153.027892)
];
var Track = new google.maps.Polyline ({
path: geo_path,
strokeColor: "# FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
Track.setMap (map);
}
I would like to add a button with an OnClick handler that would call
HTMLWindow2.execScript ('DrawTrack ()', 'JavaScript')
The function would create a line of 2-um coordinates.
How can I achieve my goal?
You must rewrite your JavaScript function to accept parameters
function DrawTrack (FromLat, FromLng, ToLat, ToLng) {
var geo_path = new Array ();
var geo_path = [
new google.maps.LatLng (FromLat, FromLng),
new google.maps.LatLng (ToLat, ToLng)
];
var Track = new google.maps.Polyline ({
path: geo_path,
strokeColor: "# FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
Track.setMap (map);
}
and then pass the values from delphi in this way
HTMLWindow2.execScript(Format('DrawTrack (%s,%s,%s,%s)',['-18.142599','178.431','-27.46758','153.027892']), 'JavaScript');