Sunday 15 June 2014

Google Maps calculate a heading which bisects two known headings -



Google Maps calculate a heading which bisects two known headings -

i using google.maps.geometry.spherical.computeheading method google maps calculate headings of paths of polygon in js.

what want calculate heading between each 2 headings.

for example, if heading of first path 45 , sec 135, heading i'm looking should 90. or if first heading 350 , sec 90, heading i'm looking 40.

i know i'm missing basic geometry here (was never @ math in school), appreciate able point me in right direction.

additionally if knows of more elegant method heading between 2 paths of polygon, open suggestions.

update:

i found way solve problem. it's not elegant similar reply in suggested previous question.

/* * h1 = first heading * h2 = sec heading * */ if (h1 < 0) { h1 = 360 - math.abs(h1); } h2 = (h2 >= 180) ? h2 - 180 : h2 + 180; // average of both headings (i.e. middle) bisect = ((h2 + h1) / 2); // if bisect > h1, need inverse if (bisect > h1) { bisect = bisect -180; }

basic answer:

keep numbers in range -180 +180, calculate shortest distance (will less 180; 180 special case, has 2 answers, both directions perpendicular line)

assuming centermark @ mutual point of 2 lines, angle1marker , angle2marker create lines point.

var hdg1 = google.maps.geometry.spherical.computeheading(centermark.getposition(), angle1marker.getposition()); var hdg2 = google.maps.geometry.spherical.computeheading(centermark.getposition(), angle2marker.getposition()); if (hdg1 > 180) hdg1 -= 360; if (hdg1 <= -180) hdg1 += 360; if (hdg2 > 180) hdg2 -= 360; if (hdg2 <= -180) hdg2 += 306; var deltahdg = (hdg1 - hdg2); if (deltahdg > 180) deltahdg -= 360; if (deltahdg <= -180) deltahdg += 360; var bisecthdg = deltahdg / 2 + hdg2; var bisectposn = google.maps.geometry.spherical.computeoffset(centermark.getposition(), 1000, bisecthdg);

code snippet:

class="snippet-code-js lang-js prettyprint-override">var map; var infowindow = new google.maps.infowindow(); var bisectline = null; var bisectmark = null; var centermark = null; var angle1marker = null; var angle2marker = null; function initialize() { geocoder = new google.maps.geocoder(); var latlng = new google.maps.latlng(-34.397, 150.644); var mapoptions = { zoom: 15, center: latlng } map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); centermark = new google.maps.marker({ map: map, position: map.getcenter() }); angle1marker = new google.maps.marker({ map: map, draggable: true, position: google.maps.geometry.spherical.computeoffset(map.getcenter(), 1000, 45) }); google.maps.event.addlistener(angle1marker, 'dragend', bisectangle); angle2marker = new google.maps.marker({ map: map, draggable: true, position: google.maps.geometry.spherical.computeoffset(map.getcenter(), 1000, 30) }); google.maps.event.addlistener(angle2marker, 'dragend', bisectangle); var poly1 = new google.maps.polyline({ path: [centermark.getposition(), angle1marker.getposition()], map: map }); poly1.binder = new mvcarraybinder(poly1.getpath()); angle1marker.bindto('position', poly1.binder, '1'); var poly2 = new google.maps.polyline({ path: [centermark.getposition(), angle2marker.getposition()], map: map }); poly2.binder = new mvcarraybinder(poly2.getpath()); angle2marker.bindto('position', poly2.binder, '1'); var bounds = new google.maps.latlngbounds(); bounds.extend(centermark); bounds.extend(angle1marker); bounds.extend(angle2marker); map.fitbounds(bounds); } google.maps.event.adddomlistener(window, 'load', initialize); function bisectangle() { var hdg1 = google.maps.geometry.spherical.computeheading(centermark.getposition(), angle1marker.getposition()); var hdg2 = google.maps.geometry.spherical.computeheading(centermark.getposition(), angle2marker.getposition()); if (hdg1 > 180) hdg1 -= 360; if (hdg1 <= -180) hdg1 += 360; if (hdg2 > 180) hdg2 -= 360; if (hdg2 <= -180) hdg2 += 306; var deltahdg = (hdg1 - hdg2); if (deltahdg > 180) deltahdg -= 360; if (deltahdg <= -180) deltahdg += 360; var bisecthdg = deltahdg / 2 + hdg2; document.getelementbyid('info').innerhtml = "a1=" + hdg1.tofixed(2) + ", a2=" + hdg2.tofixed(2) + ", ba=" + bisecthdg.tofixed(2); var bisectposn = google.maps.geometry.spherical.computeoffset(centermark.getposition(), 1000, bisecthdg); if (bisectmark && bisectmark.setmap) { // bisectmark.setmap(null); bisectmark.setposition(bisectposn); } else { bisectmark = new google.maps.marker({ position: bisectposn, map: map }); } if (bisectline && bisectline.setmap) { // bisectline.setmap(null); bisectline.setpath([centermark.getposition(), bisectposn]); } else { bisectline = new google.maps.polyline({ path: [centermark.getposition(), bisectposn], map: map }); } } /* * utilize bindto allow dynamic drag of markers refresh poly. */ function mvcarraybinder(mvcarray) { this.array_ = mvcarray; } mvcarraybinder.prototype = new google.maps.mvcobject(); mvcarraybinder.prototype.get = function(key) { if (!isnan(parseint(key))) { homecoming this.array_.getat(parseint(key)); } else { this.array_.get(key); } } mvcarraybinder.prototype.set = function(key, val) { if (!isnan(parseint(key))) { this.array_.setat(parseint(key), val); } else { this.array_.set(key, val); } } class="snippet-code-css lang-css prettyprint-override">html, body, #map-canvas { height: 100%; width: 100%; margin: 0px; padding: 0px } class="snippet-code-html lang-html prettyprint-override"><script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry&ext=.js"></script> <div id="info"></div> <div id="map-canvas"></div>

google-maps google-maps-api-3

No comments:

Post a Comment