1

Using VF i am displaying google map based on geolocation field which is a formula field(Lat_Long_Values__c) contain latitude and longitude value. When i run this VF page if there is no value in geolocation field it is throwing an error message but when i checked this under Account record it is not throwing an error message if there is no value in geolocaiton field. How to display the error message here i have attahced the VF code.

enter image description here enter image description here

<apex:page standardController="Account" >
<apex:pageBlock >
<head>

<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 

$(document).ready(function() {

  var myOptions = {
    zoom: 20,
    mapTypeId: google.maps.MapTypeId.HYBRID,
    mapTypeControl: true
  }

  var map;
  var marker;

  var geocoder = new google.maps.Geocoder();
<!--var address =  "{!Account.BillingStreet}, {!Account.BillingPostalCode} {!Account.BillingCity}, {!Account.BillingState},{!Account.BillingCountry}"; -->

   <!-- //var address =  "{!Account.Latitude__c}, {!Account.Longitude__c} ";  -->
     var address = "{!Account.Lat_Long_Values__c}";


  var infowindow = new google.maps.InfoWindow({
    content: "<b>{!Account.Lat_Long_Values__c}</b>"
 <!--  content: "<b>{!Account.BillingStreet} {!Account.BillingPostalCode} {!Account.BillingCity}, {!Account.BillingState},{!Account.BillingCountry}</b>"-->
  });

  geocoder.geocode( { address: address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results.length) {
      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {

        //create map
        map = new google.maps.Map(document.getElementById("map"), myOptions);

        //center map
        map.setCenter(results[0].geometry.location);

        //create marker
        marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map,
            title: "{!Account.Name}"
        });

        //add listeners
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);
        });
        google.maps.event.addListener(infowindow, 'closeclick', function() {
          map.setCenter(marker.getPosition()); 
        });
        google.map.setOptions({streetViewControl: false});

      }

    } else {
      $('#map').css({'height' : '15px'});
      $('#map').html("Oops! {!Account.Name}'s address could not be found, please make sure the address is correct.");
      resizeIframe();
    }
  });

  function resizeIframe() {
    var me = window.name;
    if (me) {
      var iframes = parent.document.getElementsByName(me);
      if (iframes && iframes.length == 1) {
        height = document.body.offsetHeight;
        iframes[0].style.height = height + "px";
      }
    }
  }

});
</script>

<style>
#map {
  font-family: Arial;
  font-size:12px;
  line-height:normal !important;
  height:500px;
  background:transparent;
}
</style>

</head>

<body>
<div id="map"></div> 
</body> 
</apex:pageBlock>
</apex:page>
  • Not clear what's your exact problem. Your problem is "how to display an error message" or "google map not showing correctly"? – highfive Jun 17 '14 at 5:13
  • thanks for reply highfive how to display an error message under the Account record. – user7292 Jun 17 '14 at 6:02
  • The apostrophe mark after the {!Account.Name} in line $('#map').html("Oops! {!Account.Name}'s address... may breaking your code. Try escaping special characters; i.e $('#map').html("Oops! {!Account.Name}\'s address... – highfive Jun 17 '14 at 6:09
  • Hi Highfive i have removed {!Account.Name}'s and run a code it showing empty map instead of displaying error message like the screen shots i updated above. – user7292 Jun 17 '14 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.