5

I have a list of polylines, just like google maps does when I click on the polyline I want an infowindow to show up just where I clicked, and it works just fine with this function

function mapsInfoWindow(polyline, content) {
    google.maps.event.addListener(polyline, 'click', function(event) {            
        infowindow.content = content;
        infowindow.position = event.latLng;
        infowindow.open(map);
    });
}

the problem comes when I click on the list(using the same function for that), event obviously doesn't have the latLng, but I'd like infowindow to show up in the middle of the polyline anyway, just like it does when you click on the list in the google maps link I mentioned before.

Tried LatLngBounds(); but that gives the actuall center of the area the polylines create, not the middle I need.

Any idea how to do it?

fxck
  • 4,898
  • 8
  • 56
  • 94
  • I have no ready-to-use solution, but here is the clue: (http://rbrundritt.wordpress.com/2008/10/14/calculate-midpoint-of-polyline/) It would be appreciated if you post your solution as answer. Cheers. – dmitry Oct 19 '11 at 10:08
  • Was really mainly wondering whether the api itself can help with it.. apparently it doesn't. I'll look into that link, thanks. – fxck Oct 19 '11 at 11:14
  • http://gis.stackexchange.com/questions/2128/algorithm-for-finding-irrregular-polygon-centroid-label-point more clues – fxck Oct 19 '11 at 11:24
  • I think that it was meant to be some middle point in a polyline, rather than centroid of a polygon. It is not the same. – dmitry Oct 19 '11 at 11:55
  • And, furthemore. I'm trying to maintain [extensions](http://code.google.com/r/dmitryrevenko-google-maps-v3-extensions/source/browse/) for gmaps v3, your code for google.maps.Polyline.getMidPoint() would be useful. If you'll code it, of course :) – dmitry Oct 19 '11 at 11:59

5 Answers5

5

So this is the(bit hacky) solution.

Use http://www.geocodezip.com/scripts/v3_epoly.js library, then count the total length of you polyline(various ways), divide it in half and call epoly's .GetPointsAtDistance() function upon it.

This should return LatLng point, but it acts a bit weird sometimes, returning two points or even turning that point somehow "broken". So the most secure thing you can do is probably this:

var pointInHalf = polyline.GetPointsAtDistance(polylineLength);
var pointCoordinate = new google.maps.LatLng(pointInHalf[0].lat(), pointInHalf[0].lng());

Well, better than nothing.

fxck
  • 4,898
  • 8
  • 56
  • 94
4

From http://www.geocodezip.com/v3_polyline_example_geodesic_proj.html

Without extensions and assuming the polyline is a straight line.

It is possible to convert the lat/lng coordinates to point plane (x,y) postions and calculate the average between the two. This will give you a central pixel position. You can then convert this position back to a latlng for map plotting.

var startLatLng = startMarker.getPosition(); 
var endLatLng = endMarker.getPosition(); 
var startPoint = projection.fromLatLngToPoint(startLatLng); 
var endPoint = projection.fromLatLngToPoint(endLatLng); 
// Average 
var midPoint = new google.maps.Point( 
    (startPoint.x + endPoint.x) / 2, 
    (startPoint.y + endPoint.y) / 2); 
// Unproject 
var midLatLng = projection.fromPointToLatLng(midPoint); 
var midMarker = createMarker(midLatLng, "text");

More information on changing the projection http://code.google.com/apis/maps/documentation/javascript/reference.html#Projection

Rafael Ribeiro
  • 59
  • 1
  • 11
Shane Best
  • 515
  • 5
  • 20
  • 2
    Then try wording your question a little better. – Shane Best Feb 04 '12 at 23:08
  • The very same link I just gave you is in the first post as well ;) it's also pointed out in most of the other post that it's not a straight line http://img62.imageshack.us/img62/2145/nonot.png – fxck Feb 07 '12 at 09:19
  • This would work if {"geodesic": false}, do we have a way to find the mid point of the polyline when "geodesic" is set to true? – Rahul R Jul 20 '20 at 12:39
3

So firstly you need to use the geometry library which calculates distances. Add libraries=geometry to your JS call, e.g.

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry"></script>

Assuming you know the start point and end point for your polyline, you should be able to do this:

var inBetween = google.maps.geometry.spherical.interpolate(startLatlng, endLatlng, 0.5);  
infowindow.position = inBetween;

I guess if you don't already know the start and end points, you could work it out from polyline.getPath().

duncan
  • 31,401
  • 13
  • 78
  • 99
  • Nope, that's exactly the same as using the LatLngBounds().. perhaps that (centroid?) confused you? http://img62.imageshack.us/img62/2145/nonot.png – fxck Oct 19 '11 at 22:57
  • ..and if I **actually** wanted to do this, not sure which would be better, this requires you to include another library, the LatLngBounds() on the other hand requires you to collect an array of all the latLngs and call a getCenter() function upon it, hard to say which is more effecient :) – fxck Oct 19 '11 at 23:16
  • I don't understand your screenshot – duncan Oct 20 '11 at 08:39
  • Look at this http://g.co/maps/fvm62 click on "Běžecká stopa Mílovská" on the left side, look where the infowindow appear, I want it to appear there as well, but using your code, or the LatLngBounds, it appears there img62.imageshack.us/img62/2145/nonot.png – fxck Oct 20 '11 at 08:50
  • try using computeLength() and pass it the full path of your polyline – duncan Oct 20 '11 at 09:02
  • Ooook.. computeLength() is function of what, that geometry library? edit: yes it is, but that just calculates the path's length, how could that possibly help? – fxck Oct 20 '11 at 17:42
  • so, you want to find the point in the middle of the path. first you need to know how long the path is. once you know that, half it. then somehow you have to traverse your paths until you reach that distance from the start. – duncan Oct 20 '11 at 21:18
  • How could I traverse it? I **can** traverse the points that polyline consists of, but that wouldn't necessarily give me the actual center I want(imagine polyline with only two points).. – fxck Dec 19 '11 at 16:18
  • Ah well.. I remember seeing a car driving on a polyline, so it gotta be possible. Gonna research some more. – fxck Dec 19 '11 at 16:19
2

to get the coordinates of your polyline you should do:

var widePath = new google.maps.Polyline({
path: waypointsCoordinates,
strokeColor: '#3366FF',
strokeOpacity: 0.0,
editable: true,
draggable: true,                       
strokeWeight: 3
});

and do:

var latLng [];
latLng = widePath.getPath().getArray();
duncan
  • 31,401
  • 13
  • 78
  • 99
  • 1
    It's been four years or more, I imagine there's indeed a better way to do this now. – fxck Sep 15 '15 at 15:04
2

Might be a bit old as well, but why not add the infobox on the click?

infowindow.setPosition(event.latLng);
infowindow.open(this.getMap());

If it's a click that is.

Richard Housham
  • 1,525
  • 2
  • 15
  • 31