Wednesday, July 28, 2010

Getting Longitude and Latitude using JavaScript for Microsoft Dynamics CRM 4.0 Part 2

In my previous post I described how to make possible getting Longitude and Latitude. But chosen webservice was not able to resolve some of addresses. In this post I will describe how to get coordinates using Google API.


RefreshCoords = function()
{
var address = '';
if (crmForm.all.address1_line1.DataValue != null)
address += crmForm.all.address1_line1.DataValue;

if (crmForm.all.address1_line2.DataValue != null)
address += (address == '' ? '' : ', ') + crmForm.all.address1_line2.DataValue;

if (crmForm.all.address1_city.DataValue != null)
address += (address == '' ? '' : ', ') + crmForm.all.address1_city.DataValue;

if (crmForm.all.address1_stateorprovince.DataValue != null)
address += (address == '' ? '' : ', ') + crmForm.all.address1_stateorprovince.DataValue;

if (crmForm.all.address1_postalcode.DataValue != null)
address += (address == '' ? '' : ', ') + crmForm.all.address1_postalcode.DataValue;

try
{
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("GET", "http://maps.google.com/maps/api/geocode/xml?sensor=false&address=" + address, false);
xHReq.Send(null);

var resultXml =xHReq.responseXML;
var addressComponents = resultXml.selectNodes("//GeocodeResponse/result/geometry/location");
if (addressComponents != null && addressComponents.length > 0)
{
var long = parseFloat(resultXml.selectSingleNode("//GeocodeResponse/result/geometry/location/lng").nodeTypedValue);
var lat = parseFloat(resultXml.selectSingleNode("//GeocodeResponse/result/geometry/location/lat").nodeTypedValue);

crmForm.all.address1_latitude.DataValue = lat;
crmForm.all.address1_longitude.DataValue = long;
crmForm.all.address1_latitude.ForceSubmit = true;
crmForm.all.address1_longitude.ForceSubmit = true;
}

}
catch(e){}
}

6 comments:

  1. Thank you for interesting piece of code, I've always wanted to do it for my GMaps integration, but didn't have time yet.. Once again, thank you!

    ReplyDelete
  2. Hi,

    thank you fpr ouyr post!!!
    But I have got a warning that the data will be send to other server (Internet Explorer warning)
    And if I use https connetction to my CRM Server I have got no answer from the google

    Semen

    ReplyDelete
  3. Hello Sebastian (or Semen),
    Regarding warning - it depends on your security settings of IE. You can configure it not to be displayed.

    ReplyDelete
  4. Thanks!
    But I don't know wheather it works for https.
    Under https I dont't get a popup-warning-window and I don't get the answer from google/bing (see the Part3)

    ReplyDelete
  5. Hi Andriy. Thanks for the code, this is very useful. I have the code below in the onSave event of a service activity. For some reason I am getting a permission denied error. Any thoughts?

    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
    var myAddress;
    myAddress = crmForm.all.location.DataValue;
    xHReq.Open("GET", "http://maps.google.com/maps/api/geocode/xml?sensor=false&address=" + myAddress, false);
    xHReq.Send(null);
    var resultXml =xHReq.responseXML;
    var addressComponents = resultXml.selectNodes("//GeocodeResponse/result/geometry/location");
    if (addressComponents != null && addressComponents.length > 0)
    {
    var long = parseFloat(resultXml.selectSingleNode("//GeocodeResponse/result/geometry/location/lng").nodeTypedValue);
    var lat = parseFloat(resultXml.selectSingleNode("//GeocodeResponse/result/geometry/location/lat").nodeTypedValue); crmForm.all.seg_coordinates.DataValue = lat + ", " + long;
    alert("Lat: " + lat);
    }

    ReplyDelete