TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
<!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"
>
//Global variable declare
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;
//On Map load
function
initMap() {
alert(
"initMap called"
);
//initialy map
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 marker = new google.maps.Marker({
// position: center,
// map: map
//});
//show multiple marker on MAP
//array of data
var
locations = [];
locations= GetdataaddinArray();
debugger
;
// start loop through array and map marker
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));
}
}
}
//end loop through
}
function
GetdataaddinArray()
{
var
rootAccountID = window.parent.Xrm.Page.data.entity.getId();
// Construct FetchXML for contacts in this account and its child accounts
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 +
"' />"
;
//alert("before fetchXml");
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();
// alert(serverURL);
var
Query = entityname +
"?fetchXml="
+ fetch;
var
req =
new
XMLHttpRequest();
req.open(
"GET"
, parent.Xrm.Page.context.getClientUrl() +
"/api/data/v9.0/"
+ Query,
false
);
//Removed api from url domain
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"
)
// debugger; // Try to debug in this point to get the count if any error you get
var
results = JSON.parse(req.response);
//var locations = [];
for
(
var
i = 0; i < results.value.length; i++) {
var
result = {};
result.Address1_Composite = results.value[i].address1_composite;
// var AccountId = results.value[i].accountid;
//var Address1_Composite = results.value[i].address1_composite;
// var Address1_Line1 = results.value[i].address1_line1;
// result.Address1_Composite = results.value[i].address1_postalcode;
//var Address1_Country = results.value[i].address1_country;
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;
}
//do what you need here
resultSet.push(result);
//resultSet.push(resul);
}
//var lat=null;
//var lng=null;
//resultSet.push(
//{AccountId:AccountId,
//Address1_Composite:Address1_Composite,
//Address1_Line1:Address1_Line1,
//Address1_Country:Address1_Country,
//lat:lat,
//lng:lng}
//);
}
debugger
;
alert(resultSet);
return
resultSet;
}
}
}
function
getLatitudeLongitude(callback, address) {
// If adress is not supplied, use default value 'Ferrol, Galicia, Spain'
address = address ||
'Ferrol, Galicia, Spain'
;
// Initialize the Geocoder
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>
Reply
Answers (
1
)
Using Javascript need of recursive html table tree
How to rewrite rules for these two subdomains in web.config?