खास जानकारी
इस दस्तावेज़ में बताई गई सुविधाएं, सिर्फ़ google.maps.geometry लाइब्रेरी में उपलब्ध हैं. Maps JavaScript API लोड करने पर, यह लाइब्रेरी डिफ़ॉल्ट रूप से लोड नहीं होती. हालांकि, libraries बूटस्ट्रैप पैरामीटर का इस्तेमाल करके, इसे साफ़ तौर पर बताया जाना चाहिए. ज़्यादा जानकारी के लिए, लाइब्रेरी की खास जानकारी देखें.
Maps JavaScript API की ज्यामिति लाइब्रेरी, पृथ्वी की सतह पर ज्यामितीय डेटा की गणना करने के लिए, उपयोगिता फ़ंक्शन उपलब्ध कराती है. लाइब्रेरी में तीन नेमस्पेस शामिल हैं:
sphericalमें स्फ़ेरिकल ज्यामिति की सुविधाएं शामिल हैं. इनकी मदद से, अक्षांश और देशांतर से कोण, दूरी, और क्षेत्रफल का हिसाब लगाया जा सकता है.encodingमें, एन्कोड की गई पॉलीलाइन एल्गोरिदम के मुताबिक, पॉलीलाइन पाथ को एन्कोड और डिकोड करने के लिए सुविधाएं मौजूद हैं.polyमें पॉलीगॉन और पॉलीलाइन से जुड़ी गणनाओं के लिए, यूटिलिटी फ़ंक्शन शामिल हैं.
google.maps.geometry लाइब्रेरी में कोई क्लास नहीं होती है. इसके बजाय, लाइब्रेरी में ऊपर दिए गए नेमस्पेस के स्टैटिक तरीके होते हैं.
स्फ़ेरिकल जियॉमेट्री के कॉन्सेप्ट
Maps JavaScript API में मौजूद इमेज, टू-डाइमेंशनल और "फ़्लैट" होती हैं. हालांकि, पृथ्वी थ्री-डाइमेंशनल है और इसे अक्सर चपटे गोलाकार या गोले के तौर पर दिखाया जाता है. Maps API में, हम एक गोले का इस्तेमाल करते हैं. साथ ही, Maps API, धरती को दो डाइमेंशन वाली फ़्लैट सतह पर दिखाने के लिए, प्रोजेक्शन का इस्तेमाल करता है. जैसे, आपके कंप्यूटर की स्क्रीन.
2D प्रोजेक्शन में, कभी-कभी चीज़ें जैसी दिखती हैं वैसी होती नहीं हैं. मैप प्रोजेक्शन में कुछ हद तक डिस्टॉर्शन (विकृति) ज़रूरी होता है. इसलिए, आम तौर पर यूक्लिडियन ज्यामिति लागू नहीं होती. उदाहरण के लिए, किसी गोले पर दो बिंदुओं के बीच की सबसे कम दूरी एक सीधी रेखा नहीं, बल्कि एक बड़ा वृत्त (एक प्रकार का जियोडेसिक) होती है. साथ ही, किसी गोले की सतह पर त्रिभुज बनाने वाले कोणों का योग 180 डिग्री से ज़्यादा होता है.
इन अंतरों की वजह से, किसी गोले (या उसके प्रोजेक्शन) पर ज्यामितीय फ़ंक्शन के लिए, दूरी, हेडिंग, और क्षेत्रफल जैसी चीज़ों का हिसाब लगाने के लिए, स्फ़ेरिकल ज्यामिति का इस्तेमाल करना ज़रूरी होता है. इन गोलाकार ज्यामितीय कंस्ट्रक्ट को कैलकुलेट करने के लिए, Maps API के google.maps.geometry.spherical नेमस्पेस में यूटिलिटी मौजूद होती हैं. यह नेमस्पेस, गोलाकार निर्देशांकों (अक्षांश और देशांतर) से स्केलर वैल्यू की गणना करने के लिए स्टैटिक तरीके उपलब्ध कराता है.
दूरी और क्षेत्रफल से जुड़े फ़ंक्शन
दो बिंदुओं के बीच की दूरी, उनके बीच के सबसे छोटे रास्ते की लंबाई होती है. इस सबसे छोटे रास्ते को जियोडेसिक कहा जाता है. एक गोले पर सभी जियोडेसिक,
एक बड़े वृत्त के खंड होते हैं. इस दूरी का हिसाब लगाने के लिए, computeDistanceBetween() को कॉल करें और उसे दो LatLng ऑब्जेक्ट पास करें.
अगर आपके पास कई जगहें हैं, तो किसी पाथ की लंबाई का हिसाब लगाने के लिए,
computeLength() का इस्तेमाल किया जा सकता है.
दूरी के नतीजे, मीटर में दिखाए जाते हैं.
किसी पॉलीगॉन के क्षेत्रफल (वर्ग मीटर में) का हिसाब लगाने के लिए, computeArea() को कॉल करें. साथ ही, क्लोज़्ड लूप तय करने वाले LatLng ऑब्जेक्ट का कलेक्शन पास करें.
नेविगेशन फ़ंक्शन
किसी गोले पर नेविगेट करते समय, हेडिंग किसी तय रेफ़रंस पॉइंट से दिशा का ऐंगल होता है. आम तौर पर, यह पॉइंट ट्रू नॉर्थ होता है. Google Maps API में, हेडिंग को ट्रू नॉर्थ से डिग्री में तय किया जाता है. हेडिंग को ट्रू नॉर्थ (0 डिग्री) से घड़ी की सुई की दिशा में मापा जाता है. computeHeading() तरीके का इस्तेमाल करके, दो जगहों के बीच की हेडिंग का हिसाब लगाया जा सकता है. इसके लिए, आपको दो from और to LatLng ऑब्जेक्ट पास करने होंगे.
अगर आपको कोई खास हेडिंग, शुरुआत की जगह, और तय की जाने वाली दूरी (मीटर में) पता है, तो computeOffset() का इस्तेमाल करके, डेस्टिनेशन के निर्देशांकों का पता लगाया जा सकता है.
दो LatLng ऑब्जेक्ट और 0 से 1 के बीच की वैल्यू देने पर, उनके बीच की दूरी का हिसाब भी लगाया जा सकता है. इसके लिए, interpolate() तरीके का इस्तेमाल करें. यह तरीका, दो जगहों के बीच स्फ़ेरिकल लीनियर इंटरपोलेशन करता है. इसमें वैल्यू, शुरुआती जगह से मंज़िल तक के रास्ते पर तय की जाने वाली दूरी के अंश को दिखाती है.
इस उदाहरण में, मैप पर दो पॉइंट पर क्लिक करने पर दो पॉलीलाइन बनती हैं. इनमें से एक जियोडेसिक लाइन होती है और दूसरी "सीधी" लाइन होती है, जो दोनों जगहों को जोड़ती है. साथ ही, यह दोनों पॉइंट के बीच यात्रा करने के लिए हेडिंग का हिसाब लगाती है:
TypeScript
// This example requires the Geometry library. Include the libraries=geometry // parameter when you first load the API. For example: // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"> let marker1: google.maps.Marker, marker2: google.maps.Marker; let poly: google.maps.Polyline, geodesicPoly: google.maps.Polyline; function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 4, center: { lat: 34, lng: -40.605 }, } ); map.controls[google.maps.ControlPosition.TOP_CENTER].push( document.getElementById("info") as HTMLElement ); marker1 = new google.maps.Marker({ map, draggable: true, position: { lat: 40.714, lng: -74.006 }, }); marker2 = new google.maps.Marker({ map, draggable: true, position: { lat: 48.857, lng: 2.352 }, }); const bounds = new google.maps.LatLngBounds( marker1.getPosition() as google.maps.LatLng, marker2.getPosition() as google.maps.LatLng ); map.fitBounds(bounds); google.maps.event.addListener(marker1, "position_changed", update); google.maps.event.addListener(marker2, "position_changed", update); poly = new google.maps.Polyline({ strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 3, map: map, }); geodesicPoly = new google.maps.Polyline({ strokeColor: "#CC0099", strokeOpacity: 1.0, strokeWeight: 3, geodesic: true, map: map, }); update(); } function update() { const path = [ marker1.getPosition() as google.maps.LatLng, marker2.getPosition() as google.maps.LatLng, ]; poly.setPath(path); geodesicPoly.setPath(path); const heading = google.maps.geometry.spherical.computeHeading( path[0], path[1] ); (document.getElementById("heading") as HTMLInputElement).value = String(heading); (document.getElementById("origin") as HTMLInputElement).value = String( path[0] ); (document.getElementById("destination") as HTMLInputElement).value = String( path[1] ); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example requires the Geometry library. Include the libraries=geometry // parameter when you first load the API. For example: // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"> let marker1, marker2; let poly, geodesicPoly; function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 4, center: { lat: 34, lng: -40.605 }, }); map.controls[google.maps.ControlPosition.TOP_CENTER].push( document.getElementById("info"), ); marker1 = new google.maps.Marker({ map, draggable: true, position: { lat: 40.714, lng: -74.006 }, }); marker2 = new google.maps.Marker({ map, draggable: true, position: { lat: 48.857, lng: 2.352 }, }); const bounds = new google.maps.LatLngBounds( marker1.getPosition(), marker2.getPosition(), ); map.fitBounds(bounds); google.maps.event.addListener(marker1, "position_changed", update); google.maps.event.addListener(marker2, "position_changed", update); poly = new google.maps.Polyline({ strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 3, map: map, }); geodesicPoly = new google.maps.Polyline({ strokeColor: "#CC0099", strokeOpacity: 1.0, strokeWeight: 3, geodesic: true, map: map, }); update(); } function update() { const path = [marker1.getPosition(), marker2.getPosition()]; poly.setPath(path); geodesicPoly.setPath(path); const heading = google.maps.geometry.spherical.computeHeading( path[0], path[1], ); document.getElementById("heading").value = String(heading); document.getElementById("origin").value = String(path[0]); document.getElementById("destination").value = String(path[1]); } window.initMap = initMap;
कोड में बदलने के तरीके
Maps JavaScript API में पाथ को अक्सर LatLng ऑब्जेक्ट के Array के तौर पर तय किया जाता है. हालांकि, इस तरह के ऐरे को पास करना
अक्सर मुश्किल होता है. इसके बजाय, किसी पाथ को कंप्रेस करने के लिए, Google के पॉलीलाइन एन्कोडिंग एल्गोरिदम का इस्तेमाल किया जा सकता है. इसे बाद में डिकोड करके डीकंप्रेस किया जा सकता है.
geometry लाइब्रेरी में, पॉलीलाइन को एन्कोड और डिकोड करने वाली यूटिलिटी के लिए, encoding नेमस्पेस शामिल है.
स्टैटिक मेथड encodePath(), दिए गए पाथ को कोड में बदलता है.
LatLngs का कलेक्शन या MVCArray (जो Polyline.getPath() से मिलता है) पास किया जा सकता है.
एन्कोड किए गए पाथ को डिकोड करने के लिए, decodePath() को कॉल करें. इसके बाद, एन्कोड की गई स्ट्रिंग को पास करें.
यहां दिए गए उदाहरण में, ऑक्सफ़ोर्ड, मिसिसिपी का मैप दिखाया गया है. मैप पर क्लिक करने से, पॉलीलाइन में एक पॉइंट जुड़ जाता है. पॉलीलाइन बनने के बाद, उसकी एन्कोडिंग नीचे दिखती है.
TypeScript
// This example requires the Geometry library. Include the libraries=geometry // parameter when you first load the API. For example: // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"> function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 14, center: { lat: 34.366, lng: -89.519 }, } ); const poly = new google.maps.Polyline({ strokeColor: "#000000", strokeOpacity: 1, strokeWeight: 3, map: map, }); // Add a listener for the click event google.maps.event.addListener(map, "click", (event) => { addLatLngToPoly(event.latLng, poly); }); } /** * Handles click events on a map, and adds a new point to the Polyline. * Updates the encoding text area with the path's encoded values. */ function addLatLngToPoly( latLng: google.maps.LatLng, poly: google.maps.Polyline ) { const path = poly.getPath(); // Because path is an MVCArray, we can simply append a new coordinate // and it will automatically appear path.push(latLng); // Update the text field to display the polyline encodings const encodeString = google.maps.geometry.encoding.encodePath(path); if (encodeString) { (document.getElementById("encoded-polyline") as HTMLInputElement).value = encodeString; } } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example requires the Geometry library. Include the libraries=geometry // parameter when you first load the API. For example: // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"> function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 14, center: { lat: 34.366, lng: -89.519 }, }); const poly = new google.maps.Polyline({ strokeColor: "#000000", strokeOpacity: 1, strokeWeight: 3, map: map, }); // Add a listener for the click event google.maps.event.addListener(map, "click", (event) => { addLatLngToPoly(event.latLng, poly); }); } /** * Handles click events on a map, and adds a new point to the Polyline. * Updates the encoding text area with the path's encoded values. */ function addLatLngToPoly(latLng, poly) { const path = poly.getPath(); // Because path is an MVCArray, we can simply append a new coordinate // and it will automatically appear path.push(latLng); // Update the text field to display the polyline encodings const encodeString = google.maps.geometry.encoding.encodePath(path); if (encodeString) { document.getElementById("encoded-polyline").value = encodeString; } } window.initMap = initMap;
पॉलीगॉन और पॉलीलाइन फ़ंक्शन
ज्यामिति लाइब्रेरी के poly नेमस्पेस में, उपयोगिता फ़ंक्शन होते हैं.
इनसे यह पता चलता है कि कोई पॉइंट, पॉलीगॉन या सर्कल के अंदर है या नहीं. इसके अलावा, यह भी पता चलता है कि वह पॉइंट,
पॉलीगॉन या पॉलीलाइन के पास है या नहीं.
containsLocation()
containsLocation(point:LatLng|LatLngLiteral, polygon:Polygon|Circle|CircleLiteral)
यह पता लगाने के लिए कि कोई पॉइंट, पॉलीगॉन या सर्कल के अंदर आता है या नहीं, पॉइंट और google.maps.Polygon, google.maps.Circle या google.maps.CircleLiteral को google.maps.geometry.poly.containsLocation() में पास करें.
अगर पॉइंट, आकार के अंदर या उसके किनारे पर है, तो फ़ंक्शन true दिखाता है.
अगर उपयोगकर्ता का क्लिक, तय किए गए त्रिकोण के अंदर होता है, तो यहां दिया गया कोड ब्राउज़र कंसोल में 'सही' लिखता है. ऐसा न होने पर, यह 'गलत' लिखता है.
function initialize() { var mapOptions = { zoom: 5, center: new google.maps.LatLng(24.886, -70.269), mapTypeId: 'terrain' }; var map = new google.maps.Map(document.getElementById('map'), mapOptions); var bermudaTriangle = new google.maps.Polygon({ paths: [ new google.maps.LatLng(25.774, -80.190), new google.maps.LatLng(18.466, -66.118), new google.maps.LatLng(32.321, -64.757) ] }); google.maps.event.addListener(map, 'click', function(event) { console.log(google.maps.geometry.poly.containsLocation(event.latLng, bermudaTriangle)); }); } google.maps.event.addDomListener(window, 'load', initialize);
इस कोड का एक अन्य वर्शन, मैप पर नीला त्रिकोण बनाता है. ऐसा तब होता है, जब क्लिक बरमूडा ट्रायंगल के अंदर हो. अगर क्लिक इसके बाहर होता है, तो लाल सर्कल बनता है:
isLocationOnEdge()
isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number)
यह पता लगाने के लिए कि कोई पॉइंट, पॉलीलाइन पर या उसके आस-पास मौजूद है या नहीं, या पॉलीगॉन के किनारे पर या उसके आस-पास मौजूद है या नहीं, पॉइंट, पॉलीलाइन/पॉलीगॉन, और वैकल्पिक तौर पर डिग्री में टॉलरेंस वैल्यू को google.maps.geometry.poly.isLocationOnEdge() में पास करें. अगर पॉइंट और लाइन या किनारे पर मौजूद सबसे नज़दीकी पॉइंट के बीच की दूरी, तय की गई टॉलरेंस के अंदर आती है, तो फ़ंक्शन true दिखाता है. टॉलरेंस की डिफ़ॉल्ट वैल्यू 10-9 डिग्री होती है.
function initialize() { var myPosition = new google.maps.LatLng(46.0, -125.9); var mapOptions = { zoom: 5, center: myPosition, mapTypeId: 'terrain' }; var map = new google.maps.Map(document.getElementById('map'), mapOptions); var cascadiaFault = new google.maps.Polyline({ path: [ new google.maps.LatLng(49.95, -128.1), new google.maps.LatLng(46.26, -126.3), new google.maps.LatLng(40.3, -125.4) ] }); cascadiaFault.setMap(map); if (google.maps.geometry.poly.isLocationOnEdge(myPosition, cascadiaFault, 10e-1)) { alert("Relocate!"); } } google.maps.event.addDomListener(window, 'load', initialize);
जियोफ़ेंस और सुरक्षित ज़ोन की रेडियस की जांच
मैप पर कोई लाइव कोऑर्डिनेट (जैसे, किसी वाहन, ऐसेट या उपयोगकर्ता की जगह की जानकारी) जियोफ़ेंस या सेफ़-ज़ोन के दायरे में है या नहीं, यह देखने के लिए मैप के आकार को geometry लाइब्रेरी के साथ जोड़ें:
- सर्कुलर जियोफ़ेंस (दायरे की जांच):
google.maps.Circleकी मदद से पेरीमीटर बनाएं (याgoogle.maps.CircleLiteralतय करें) और टारगेट कोऑर्डिनेट और सर्कल कोgoogle.maps.geometry.poly.containsLocation()में पास करें. अगर आपकोCircle.getCenter()से मीटर में दूरी भी चाहिए, तो टारगेट कोऑर्डिनेट और सेंटर कोgoogle.maps.geometry.spherical.computeDistanceBetween()पर पास करें. - पॉलीगॉन जियोफ़ेंस:
google.maps.Polygonकी मदद से पेरीमीटर बनाएं. इसके बाद, टारगेट निर्देशांक और पॉलीगॉन कोgoogle.maps.geometry.poly.containsLocation()पर पास करें.