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
federico Lopezfederico Lopez 

how do i retrieve the latitude and longitud from an address and add show it in a map with visualforce?

i need to retrieve the coordinates from address and show them into a map

this is my code i want to be able to show coordinates in the map markers.

<apex:page standardController="Cuenta__c">

  <apex:pageBlock >
      


    
  <apex:map width="600px" height="400px" mapType="roadmap"
    center="{!Cuenta__c.Billing_Address__c},{!Cuenta__c.Billing_City__c},{!Cuenta__c.Billing_State__c}">

<apex:mapMarker title="Billing Address"
       position="{!Cuenta__c.Billing_Address__c},{!Cuenta__c.Billing_City__c},{!Cuenta__c.Billing_State__c}"
                >
      <apex:mapInfoWindow >
            <apex:outputPanel layout="block" style="font-weight: bold;">
              <apex:outputText >{!Cuenta__c.Billing_Address__c}</apex:outputText>
            </apex:outputPanel>

            <apex:outputPanel layout="block">
              <apex:outputText >{!Cuenta__C.Billing_City__C }</apex:outputText>
            </apex:outputPanel>               

            <apex:outputPanel layout="block">
              <apex:outputText >{!Cuenta__c.Billing_State__c}</apex:outputText>
            </apex:outputPanel> 
          

           
          </apex:mapInfoWindow>
      
      </apex:mapMarker>
                  <apex:mapMarker title="Fiscal Address"
       position="{!Cuenta__c.Fiscal_Address__c},{!Cuenta__c.Fiscal_City__c},{!Cuenta__c.Fiscal_State__c}"
    >
                      <apex:mapInfoWindow >
            <apex:outputPanel layout="block" style="font-weight: bold;">
              <apex:outputText >{!Cuenta__c.Fiscal_Address__c}</apex:outputText>
            </apex:outputPanel>

            <apex:outputPanel layout="block">
              <apex:outputText >{!Cuenta__C.Fiscal_City__C }</apex:outputText>
            </apex:outputPanel>               

            <apex:outputPanel layout="block">
              <apex:outputText >{!Cuenta__c.Fiscal_State__c}</apex:outputText>
            </apex:outputPanel>               

           
          </apex:mapInfoWindow>
      
      </apex:mapMarker>
      <apex:mapMarker title="Shipping Address"
       position="{!Cuenta__c.Shipping_Address__c},{!Cuenta__c.Shipping_City__c},{!Cuenta__c.Shipping_State__c}"
    >
                                <apex:mapInfoWindow >
            <apex:outputPanel layout="block" style="font-weight: bold;">
              <apex:outputText >{!Cuenta__c.Shipping_Address__c}</apex:outputText>
            </apex:outputPanel>

            <apex:outputPanel layout="block">
              <apex:outputText >{!Cuenta__C.Shipping_City__C }</apex:outputText>
            </apex:outputPanel>               

            <apex:outputPanel layout="block">
              <apex:outputText >{!Cuenta__c.Shipping_State__c}</apex:outputText>
            </apex:outputPanel>               

           
          </apex:mapInfoWindow>
      
      </apex:mapMarker>

      
      </apex:map>
  </apex:pageBlock>

</apex:page>

Khan AnasKhan Anas (Salesforce Developers) 
Hi Federico,

Greetings to you!

You can use below code:
<apex:map width="400px" height="200px" mapType="roadmap" zoomLevel="16" center="{latitude:{!Account.LatitudeTEXT__c},longitude:{!Account.LongitudeTEXT__c}}">

                     <apex:mapMarker title="{!Account.Name}" position="{latitude:{!Account.LatitudeTEXT__c},longitude:{!Account.LongitudeTEXT__c}}"/>
</apex:map>

Please refer to the below links which might help you further with the above requirement.

https://developer.salesforce.com/forums/?id=906F0000000MM56IAG

http://jessealtman.com/2015/02/new-spring-15-features-mapping/

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_maps_markers.htm

https://help.salesforce.com/articleView?id=custom_field_geolocate_overview.htm&type=0

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
karthikeyan perumalkarthikeyan perumal
Hello, 

you have to use google api to get the lat and lang from address which you have. kindly use below sample code. 
 
Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&sensor=false');
            req.setMethod('GET');
            req.setTimeout(60000);

            try{
                // callout
                HttpResponse res = h.send(req);

                // parse coordinates from response
                JSONParser parser = JSON.createParser(res.getBody());
                double lat = null;
                double lon = null;
                while (parser.nextToken() != null) {
                    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                        (parser.getText() == 'location')){
                           parser.nextToken(); // object start
                           while (parser.nextToken() != JSONToken.END_OBJECT){
                               String txt = parser.getText();
                               parser.nextToken();
                               if (txt == 'lat')
                                   lat = parser.getDoubleValue();
                               else if (txt == 'lng')
                                   lon = parser.getDoubleValue();
                           }

                    }
                }



            } catch (Exception e) {
            }

Hope this will help you. 

Thanks
karthik