Priti Kumari

Priti Kumari

  • NA
  • 6.1k
  • 5.7m

unable to add lat and lng in array dynamically

May 23 2018 5:08 AM
Problem in GetdataaddinArray funtion.i am trying to add address and lat,lng in array inside for loop.but problem is add only address in array.because i have used getLatitudeLongitude funtion to get lat and lng. getLatitudeLongitude funtion callback showresult funtion.i have dubug all code:
 
execution of code :i have placed alert inside showresult function and outside the for loop .
execute alert of outside the for loop first then execute alert of showresult funtion.
execution flow:
alert(resultSet);-1
alert(lat);-2
alert(lng);-3
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>  
  5. <script src="../../ClientGlobalContext.js.aspx"></script>  
  6. <script src="new_/Script/SDK.REST.js" type="text/javascript"></script>  
  7. <title>Map</title>  
  8. <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0" type="text/javascript"></script>  
  9. <script src="http://maps.google.com/maps/api/js?sensor=false"  
  10. type="text/javascript"></script>  
  11. </head>  
  12. <body>  
  13. <div id="map" style="width: 100vw; height: 100vh;"></div>  
  14. <div id='printoutPanel'></div>  
  15. <p id="demo"></p>  
  16. <p id="dem"></p>  
  17. <script type="text/javascript">  
  18. //Global variable declare  
  19. var key = "AIzaSyDc7kJtpSRgj5jKNdeTn246wVRGT7wrcW4";  
  20. var map = null;  
  21. var messageBox = null;  
  22. var lat = null;  
  23. var lon = null;  
  24. var City = null;  
  25. var AccountName = null;  
  26. var pushpin = null;  
  27. var composite=null;  
  28. var accid=null;  
  29. var latitude=[];  
  30. var longitude=[];  
  31. var infowindow = [];  
  32. var composite_address;  
  33. //On Map load  
  34. function initMap() {  
  35. alert("initMap called");  
  36. //initialy map  
  37. document.onreadystatechange = function (){  
  38. if(document.readyState == "complete")  
  39. {  
  40. var center = {lat: -34.397, lng: 150.644};  
  41. var map = new google.maps.Map(document.getElementById('map'), {  
  42. zoom: 17,  
  43. center: center,  
  44. mapTypeId: 'satellite',  
  45. });  
  46. debugger;  
  47. //var marker = new google.maps.Marker({  
  48. // position: center,  
  49. // map: map  
  50. //});  
  51. //show multiple marker on MAP  
  52. //array of data  
  53. var locations = [];  
  54. locations= GetdataaddinArray();  
  55. debugger;  
  56. // start loop through array and map marker  
  57. var infowindow = new google.maps.InfoWindow({});  
  58. var marker, count;  
  59. for (count = 0; count < locations.length; count++) {  
  60. marker = new google.maps.Marker({  
  61. position: new google.maps.LatLng(locations[count][1], locations[count][2]),  
  62. map: map,  
  63. title: locations[count][0]  
  64. });  
  65. google.maps.event.addListener(marker, 'click', (function (marker, count) {  
  66. return function () {  
  67. infowindow.setContent(locations[count][0]);  
  68. infowindow.open(map, marker);  
  69. }  
  70. })(marker, count));  
  71. }  
  72. }  
  73. }  
  74. //end loop through  
  75. }  
  76. function GetdataaddinArray()  
  77. {  
  78. var rootAccountID = window.parent.Xrm.Page.data.entity.getId();  
  79. // Construct FetchXML for contacts in this account and its child accounts  
  80. var fetchXml = "<fetch version='1.0' mapping='logical'>";  
  81. fetchXml += "<entity name='account'>";  
  82. fetchXml += "<attribute name='address1_composite'/>";  
  83. fetchXml += "<order attribute='address1_composite' descending='false' />";  
  84. fetchXml += "<link-entity name='account' from='accountid' to='accountid' link-type='inner' >";  
  85. fetchXml += "<filter type='and'>";  
  86. fetchXml += "<condition attribute='accountid' operator='eq-or-under' value='" + rootAccountID + "' />";  
  87. //alert("before fetchXml");  
  88. fetchXml += "<condition attribute='accountid' operator='not-null' />";  
  89. fetchXml += "</filter>";  
  90. fetchXml += "</link-entity>";  
  91. fetchXml += "</entity>";  
  92. fetchXml += "</fetch>";  
  93. alert(fetchXml);  
  94. var fetch = encodeURI(fetchXml);  
  95. var entityname = "accounts";  
  96. alert(entityname);  
  97. var serverURL = parent.Xrm.Page.context.getClientUrl();  
  98. // alert(serverURL);  
  99. var Query = entityname + "?fetchXml=" + fetch;  
  100. var req = new XMLHttpRequest();  
  101. req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + Query, false); //Removed api from url domain  
  102. req.setRequestHeader("Accept""application/json");  
  103. req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  104. req.setRequestHeader("OData-MaxVersion""4.0");  
  105. req.setRequestHeader("OData-Version""4.0");  
  106. req.send();  
  107. var resultSet = [];  
  108. debugger;  
  109. if (req.readyState == 4) {  
  110. alert("inside first if");  
  111. debugger;  
  112. if (req.status==200) {  
  113. alert("inside second if")  
  114. // debugger; // Try to debug in this point to get the count if any error you get  
  115. var results = JSON.parse(req.response);  
  116. //var locations = [];  
  117. for (var i = 0; i < results.value.length; i++) {  
  118. var result = {};  
  119. result.Address1_Composite = results.value[i].address1_composite;  
  120. // var AccountId = results.value[i].accountid;  
  121. //var Address1_Composite = results.value[i].address1_composite;  
  122. // var Address1_Line1 = results.value[i].address1_line1;  
  123. // result.Address1_Composite = results.value[i].address1_postalcode;  
  124. //var Address1_Country = results.value[i].address1_country;  
  125. if (result.Address1_Composite !== undefined)  
  126. {  
  127. getLatitudeLongitude(showResult, result.Address1_Composite);  
  128. function showResult(result) {  
  129. var lat = result.geometry.location.lat();  
  130. var lng = result.geometry.location.lng();  
  131. alert(lat);  
  132. alert(lng);  
  133. result.lat=lat;  
  134. result.lng=lng;  
  135. }  
  136. //do what you need here  
  137. resultSet.push(result);  
  138. //resultSet.push(resul);  
  139. }  
  140. //var lat=null;  
  141. //var lng=null;  
  142. //resultSet.push(  
  143. //{AccountId:AccountId,  
  144. //Address1_Composite:Address1_Composite,  
  145. //Address1_Line1:Address1_Line1,  
  146. //Address1_Country:Address1_Country,  
  147. //lat:lat,  
  148. //lng:lng}  
  149. //);  
  150. }  
  151. debugger;  
  152. alert(resultSet);  
  153. return resultSet;  
  154. }  
  155. }  
  156. }  
  157. function getLatitudeLongitude(callback, address) {  
  158. // If adress is not supplied, use default value 'Ferrol, Galicia, Spain'  
  159. address = address || 'Ferrol, Galicia, Spain';  
  160. // Initialize the Geocoder  
  161. debugger;  
  162. geocoder = new google.maps.Geocoder();  
  163. if (geocoder) {  
  164. geocoder.geocode({  
  165. 'address': address  
  166. }, function (results, status) {  
  167. if (status == google.maps.GeocoderStatus.OK) {  
  168. callback(results[0]);  
  169. }  
  170. });  
  171. }  
  172. }  
  173. </script>  
  174. <script async defer  
  175. src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDc7kJtpSRgj5jKNdeTn246wVRGT7wrcW4&callback=initMap">  
  176. </script>  
  177. </body>  
  178. </html>

Answers (1)