I have two objects called 1. Project__c, 2. Enterprise__c.
There are two fields in the Enterprise object called Latitude__c and Longitude__c There is a relationshipdefined on the Enterprise object with lookup to Project Title.
Now, I would like to create a map on the Project page layout showing all the Enterprises related to thatproject using the latitude and longitude fields.
Please find the code here:
<apex:page standardcontroller="Project__c">
This map shows the locations of enterprises which are associated with {!Project__c.Name} project.
<apex:includeScript value="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=salesforce.com"/>
<script type="text/javascript">
var map;
var geoPoints = [];
var counter = 0;
var pointCounter = 0;
function geoCodeEnd(r) {
if (r.success == 1) {
if (r.GeoPoint.Lat != 0) {
geoPoints[geoPoints.length] = r.GeoPoint;
pointCounter++;
}
} else {
pointCounter++;
}
if (pointCounter >= counter) {
var bestZoomLevel = map.getZoomLevel(geoPoints);
if (bestZoomLevel < 5) bestZoomLevel = 5;
var geoCenter = map.getCenterGeoPoint(geoPoints);
map.drawZoomAndCenter(geoCenter, bestZoomLevel);
map.panToLatLon(geoCenter);
}
}
function loadMap(domId) {
// Create a map object
map = new YMap(document.getElementById(domId));
// Display the map centered on given address
YEvent.Capture(map, EventsList.onEndGeoCode, geoCodeEnd);
map.addZoomLong();
plotMarkers();
}
function plotMarkers() {
<apex:repeat var="ja" value="{!Project__c.enterprises__r}">
counter++;
var marker = new YMarker("{!ja.Latitude__c}, {!ja.Longitude__c}");
marker.enterpriseName = "{!SUBSTITUTE(LINKTO(ja.Enterprise__r,$Action.Enterprise__c.View,ja.Enterprise__c),'"','')}";
marker.addLabel(counter);
YEvent.Capture(marker, EventsList.MouseClick,
function() {this.openSmartWindow
("<div>This is where " +
this.enterpriseName +
" is located!<BR> </div>") });
map.addOverlay(marker);
</apex:repeat>
if(counter == 0) {
// Display map of US if no Enterprises found
var myPoint = new YGeoPoint(40,-95);
map.drawZoomAndCenter(myPoint, 14);
alert("There are no enterprises for this project to map.");
}
}
</script>
<apex:outputPanel layout="block" id="mapContainer" style="height: 300px">Loading map...</apex:outputPanel>
<script>
loadMap('{!$Component.mapContainer}');
</script>
</apex:page>