How to implement Google Map in visualforce page ?

Wednesday, July 19, 2017

Implement Google Map in visualforce page


Following code works for you if you need Google Map in visualforce page,

1. If your visualforce page does not show up with the map then just replace the "<script src="http://maps.google.com/maps?file=api" />" to "<script src="https://maps.google.com/maps?file=api" />".

2. As you can see that I have used standard controller so you need to pass the account Id in URL. Let’s say if the page name is “ImplementGoogleMaps” then the URL will look something like this : “…/apex/ImplementGoogleMaps?id=YOUR_ACCOUNT_ID”.


3. When you click on the balloon(Map marker) it will show you the Account name and the address, you can change it according to your need by changing the code of line "marker.bindInfoWindowHtml"

Reference :
http://code.google.com/apis/maps/documentation/webservices/

Hope it help you out !!

<apex:page standardController="Account">

<script src="http://maps.google.com/maps?file=api" />

<script type="text/javascript">

var map = null;
var geocoder = null;

var address = "{!Account.BillingStreet}, {!Account.BillingPostalCode} {!Account.BillingCity}, {!Account.BillingState}, {!Account.BillingCountry}";

function initialize() {
     if(GBrowserIsCompatible()){
         map = new GMap2(document.getElementById("MyMap"));
         map.addControl(new GMapTypeControl());
         map.addControl(new GLargeMapControl3D());
         geocoder = new GClientGeocoder();
         geocoder.getLatLng(address,
             function(point){
                 if(!point){
                     document.getElementById("MyMap").innerHTML = address + " not found";
                 }else{
                     map.setCenter(point, 13);
                     var marker = new GMarker(point);
                     map.addOverlay(marker);
                     marker.bindInfoWindowHtml("Account Name : <b><i> {!Account.Name} </i></b> Address : "+address);
                 }
             }
         );
     }
}

</script>

<div id="MyMap" style="width:100%;height:300px" />

<script>
    initialize() ;
</script>

</apex:page>

5 comments:

Mohit Bansal said...

Admins and Developers can display Google Map with Geolocations for any Object Records using BOFC app

Mohit Bansal said...

We designed solution for our client for Google Map Integration in both lightning and classic, where user only need to select type of record he wants to search (Account, contact or any custom object). Explore detail in link - Learn to Display Google Map with Geolocations for any Object Records - BOFC

Mohit Bansal said...

We designed solution for our client for Google Map Integration in both lightning and classic, where user only need to select type of record he wants to search (Account, contact or any custom object). Explore detail in link - Learn to Display Google Map with Geolocations for any Object Records - BOFC

Salesforce Developer said...

We designed solution for our client for Google Map Integration in both lightning and classic, where user only need to select type of record he wants to search (Account, contact or any custom object). Explore detail in link - Learn to Display Google Map with Geolocations for any Object Records - BOFC

Mohit Bansal said...

Nice Post!!!!
Want to perform bulk metadata operations in few click - follow the given link - Bulk Object Field Creator