function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nelson-BenavidesNelson-Benavides 

Visualforce page with Google Maps API v3 using Geolocation data type

Hello all,

First I apologize if this has been discussed here already. I searched everywhere here and I don't seem to find what I'm trying to accomplish.

I have a Visualforce page integrated with Google Maps using API v3. I had it all working fine for awhile until we had to change the field from where we were getting the GPS coordinates.

We used to have a text field were we manually entered the GPS coordinates (both latitude and longitude) in decimal format. I had all the code to split the coordinates and make sure there was a comma in between.. etc.., all working fine.

The problem now is that instead of using the text field, we had to use the Geolocation field (data type). As you know, this is one field at the Opportunity displaying both lat and lon. When trying to input the coordinates you have to edit the page and enter lat and lon in 2 different fields.

I have changed my code to use the Geolocation field instead, but now it doesn't display the map on the VF page.

I had the code like this when using the text field (which was working just fine):
...
var latlngString = "{!Opportunity.GPS_Coordinates__c}";
     var temp = latlngString.split(',', 2);
     var lat = parseFloat(temp[0]);
     var lng = parseFloat(temp[1]);
     var latlng = new google.maps.LatLng(lat, lng);
...

The new Geolocation field is named: GPS_Location

Now that we are using the Geolocation field, I cannot do this anymore:
...
var latlngString = "{!Opportunity.GPS_Location__c}";
...
The line above gives me an error saying: "Data type not supported"

Then I read somewhere else that the latitude and longitude values can be accessed as field-name__Latitude__s and field-name__Longitude__s

So I did this:
...
var lat = {!Opportunity.GPS_Location__Latitude__s};
var lng = {!Opportunity.GPS_Location__Longitude__s};
var latlng = new google.maps.LatLng(lat, lng);
...
No errors at this point but the map is not displayed.

I'm not sure if the variables lat and lng are actually getting the coordinates.

I'm not using any custom controllers or anything, just the standardController="Opportunity"

What am I missing?

Any help I would appreciate it.

Thanks in advance.

--Nelson.
Best Answer chosen by Nelson-Benavides
Nelson-BenavidesNelson-Benavides
FYI.

I found the solution for this here (https://success.salesforce.com/answers?id=90630000000guBkAAI" target="_blank) posted by Greg Jarrett on March 3, 2014

The link above is a long thread, so here is Greg's solution:

In response to Nathan's question about doing this with GEOLOCATION fields, it can be done by converting the GeoLocation field to a text value as a separate hidden field in the object.

1. Create a custom FORMULA field (return type = TEXT) on your object. Lets call this field "coordTEXT" Make its default value equal to:

TEXT(geolocationField__Latitude__s)&" "&TEXT(geolocationField__Longitude__s)

(make sure you replace "geolocationField" with your custom field name)
(The VF page can reference this TEXT field (It is not able to reference a GEOLOCATION field for me))

2. Instead of using "var address = Billing Address, Street Address etc" in the VF page, use this:

var address = "{!Custom_Object__c.CoordTEXT__c}";

(make sure you replace "Custom_Object__c" with your own custom/standard object name, and don't forget the semi-colon at the end)


Thanks,

--Nelson.