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
BenzyBenzy 

Coordinates in Visualforce map results in different location to Google maps

I am using the apex:map component in a Visualforce page. I can successfully render a map using coordinates pulled through an apex class.

However, the resulting location on the Visualforce page is slightly different to that shown if I put the same coordinates directly into Google Maps. (See maps below)

Does anyone know why this is happening?

VF page map code
 
<!-- Display the address on a map -->
        <apex:map width="600px" height="400px" mapType="roadmap" zoomLevel="17" center="{!program.Location__r.LatitudeTEXT__c},{!program.Location__r.LongitudeTEXT__c}">
            <apex:mapMarker title="" position="{!program.Location__r.LatitudeTEXT__c},{!program.Location__r.LongitudeTEXT__c}"/>
        </apex:map>

Visualforce Page Map
(you can see the coordinates returned from the apex class just above the map - these are what the map is using.)

User-added image

Google Map
(notice coordinates are the same as the VF page, but pin location is different)

User-added image
Best Answer chosen by Benzy
Sasha_SFDCSasha_SFDC
Hi Benzy,

If you already have the coordinates for the location you're interested in, you should pass them as string that represents a JSON object with 'latitude' and 'longitude' attributes that specify location coordinates. For example, in your case it should be:

center="{latitude: 17.415886, longitude: 78.405906}",        not center="17.415886, 78.405906" as you have it now.

Another option is to use an Apex map object of type Map<String, Double>, with 'latitude' and 'longitude' keys. When you use just comma separated coordinates, the string is treated as an address string and is geocoded to determine actual latitude and longitude. It looks like geocoding returns slightly different latitude and longitude values and this causes the behavior that you observe.



 

All Answers

Sasha_SFDCSasha_SFDC
Hi Benzy,

If you already have the coordinates for the location you're interested in, you should pass them as string that represents a JSON object with 'latitude' and 'longitude' attributes that specify location coordinates. For example, in your case it should be:

center="{latitude: 17.415886, longitude: 78.405906}",        not center="17.415886, 78.405906" as you have it now.

Another option is to use an Apex map object of type Map<String, Double>, with 'latitude' and 'longitude' keys. When you use just comma separated coordinates, the string is treated as an address string and is geocoded to determine actual latitude and longitude. It looks like geocoding returns slightly different latitude and longitude values and this causes the behavior that you observe.



 
This was selected as the best answer
BenzyBenzy
Thanks Sasha

That worked perfectly.
Bjoern HaackeBjoern Haacke
Hi, I have the same problem, but newbi here, how would the VF code from up look like?
Thanks, Bjoern