google maps middle of a polyline (centroid?)

后端 未结 5 925
情深已故
情深已故 2021-01-14 19:07

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 fu

5条回答
  •  长发绾君心
    2021-01-14 19:40

    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

提交回复
热议问题