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
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
- <script src="../../ClientGlobalContext.js.aspx"></script>
- <script src="new_/Script/SDK.REST.js" type="text/javascript"></script>
- <title>Map</title>
- <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0" type="text/javascript"></script>
- <script src="http://maps.google.com/maps/api/js?sensor=false"
- type="text/javascript"></script>
- </head>
- <body>
- <div id="map" style="width: 100vw; height: 100vh;"></div>
- <div id='printoutPanel'></div>
- <p id="demo"></p>
- <p id="dem"></p>
- <script type="text/javascript">
-
- var key = "AIzaSyDc7kJtpSRgj5jKNdeTn246wVRGT7wrcW4";
- var map = null;
- var messageBox = null;
- var lat = null;
- var lon = null;
- var City = null;
- var AccountName = null;
- var pushpin = null;
- var composite=null;
- var accid=null;
- var latitude=[];
- var longitude=[];
- var infowindow = [];
- var composite_address;
-
- function initMap() {
- alert("initMap called");
-
- document.onreadystatechange = function (){
- if(document.readyState == "complete")
- {
- var center = {lat: -34.397, lng: 150.644};
- var map = new google.maps.Map(document.getElementById('map'), {
- zoom: 17,
- center: center,
- mapTypeId: 'satellite',
- });
- debugger;
-
-
-
-
-
-
- var locations = [];
- locations= GetdataaddinArray();
- debugger;
-
- var infowindow = new google.maps.InfoWindow({});
- var marker, count;
- for (count = 0; count < locations.length; count++) {
- marker = new google.maps.Marker({
- position: new google.maps.LatLng(locations[count][1], locations[count][2]),
- map: map,
- title: locations[count][0]
- });
- google.maps.event.addListener(marker, 'click', (function (marker, count) {
- return function () {
- infowindow.setContent(locations[count][0]);
- infowindow.open(map, marker);
- }
- })(marker, count));
- }
- }
- }
-
- }
- function GetdataaddinArray()
- {
- var rootAccountID = window.parent.Xrm.Page.data.entity.getId();
-
- var fetchXml = "<fetch version='1.0' mapping='logical'>";
- fetchXml += "<entity name='account'>";
- fetchXml += "<attribute name='address1_composite'/>";
- fetchXml += "<order attribute='address1_composite' descending='false' />";
- fetchXml += "<link-entity name='account' from='accountid' to='accountid' link-type='inner' >";
- fetchXml += "<filter type='and'>";
- fetchXml += "<condition attribute='accountid' operator='eq-or-under' value='" + rootAccountID + "' />";
-
- fetchXml += "<condition attribute='accountid' operator='not-null' />";
- fetchXml += "</filter>";
- fetchXml += "</link-entity>";
- fetchXml += "</entity>";
- fetchXml += "</fetch>";
- alert(fetchXml);
- var fetch = encodeURI(fetchXml);
- var entityname = "accounts";
- alert(entityname);
- var serverURL = parent.Xrm.Page.context.getClientUrl();
-
- var Query = entityname + "?fetchXml=" + fetch;
- var req = new XMLHttpRequest();
- req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + Query, false);
- req.setRequestHeader("Accept", "application/json");
- req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
- req.setRequestHeader("OData-MaxVersion", "4.0");
- req.setRequestHeader("OData-Version", "4.0");
- req.send();
- var resultSet = [];
- debugger;
- if (req.readyState == 4) {
- alert("inside first if");
- debugger;
- if (req.status==200) {
- alert("inside second if")
-
- var results = JSON.parse(req.response);
-
- for (var i = 0; i < results.value.length; i++) {
- var result = {};
- result.Address1_Composite = results.value[i].address1_composite;
-
-
-
-
-
- if (result.Address1_Composite !== undefined)
- {
- getLatitudeLongitude(showResult, result.Address1_Composite);
- function showResult(result) {
- var lat = result.geometry.location.lat();
- var lng = result.geometry.location.lng();
- alert(lat);
- alert(lng);
- result.lat=lat;
- result.lng=lng;
- }
-
- resultSet.push(result);
-
- }
-
-
-
-
-
-
-
-
-
-
- }
- debugger;
- alert(resultSet);
- return resultSet;
- }
- }
- }
- function getLatitudeLongitude(callback, address) {
-
- address = address || 'Ferrol, Galicia, Spain';
-
- debugger;
- geocoder = new google.maps.Geocoder();
- if (geocoder) {
- geocoder.geocode({
- 'address': address
- }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- callback(results[0]);
- }
- });
- }
- }
- </script>
- <script async defer
- src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDc7kJtpSRgj5jKNdeTn246wVRGT7wrcW4&callback=initMap">
- </script>
- </body>
- </html>