Just create 2 Polylines...
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 3,
center: {lat: 20, lng: 0},
mapTypeId: 'terrain'
});
var flightPlanCoordinates = [
{lat: 20, lng: -20},
{lat: 20, lng: 20}
];
new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: .3,
strokeWeight: 20,
map: map
});
new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 1,
map: map
});
}
initialize();
#map-canvas {
height: 200px;
}
<div id="map-canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js"></script>
Edit:
You mentioned in your comment you need the stroke to be fixed at a given distance from the center line.
You could still use that technique I think, as you can calculate the size in meters of a map pixel, based on the latitude and the zoom level, and as the size of the stroke is also in pixels, you should be able to recalculate the stroke each time you zoom-in/out. It won't be 100% precise since the stroke width must be an integer, but it should work.
It should work if the various Polyline points are more or less on the same latitude, because the resolution of a map with the Mercator projection is dependent on the latitude. So, for example if you have a big Polyline crossing from -70 to +70 latitude, it won't be accurate all the way.
This is the formula you can use to compute the size of 1 map pixel. You would need to replace map.getCenter().lat()
by your Polyline center point.
// Calculate the width of 1 map pixel in meters
let scale = 156543.03392 * Math.cos(map.getCenter().lat() * Math.PI / 180) / Math.pow(2, map.getZoom());
Unfortunately, there is an issue with this approach. Although it's undocumented, a Polyline stroke width can't be bigger than 32px.
var map;
var polyBounds;
var polyStroke;
var polyWidth = 50; // Width in meters
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 12,
center: {
lat: 20,
lng: 0
},
mapTypeId: 'terrain'
});
var flightPlanCoordinates = [{
lat: 20,
lng: -0.5
},
{
lat: 20,
lng: 0.5
}
];
polyStroke = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: .3,
strokeWeight: 0,
map: map
});
new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 1,
map: map
});
polyBounds = new google.maps.LatLngBounds();
for (var i = 0; i < flightPlanCoordinates.length; i++) {
polyBounds.extend(flightPlanCoordinates[i]);
}
google.maps.event.addListenerOnce(map, 'idle', setStroke);
google.maps.event.addListener(map, 'zoom_changed', setStroke);
}
function setStroke() {
// Calculate the width of 1 map pixel in meters
let scale = 156543.03392 * Math.cos(polyBounds.getCenter().lat() * Math.PI / 180) / Math.pow(2, map.getZoom());
polyStroke.setOptions({
strokeWeight: Math.ceil(polyWidth / scale)
});
}
initialize();
#map-canvas {
height: 200px;
}
<div id="map-canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js"></script>
So unless you find out that these values allow for your use case, this won't be a viable solution...
The solution you were pointed to in the comments (How to draw a polygon around a polyline in JavaScript?) is still probably your best option.