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
Milan HrdlickaMilan Hrdlicka 

Getting geolocation data filled in a visualforce page using external web service calllout

Hi all,

I have following visualforce page:

<apex:page standardController="Shop__c" extensions="myControllerExtension1" >
<apex:form >
<apex:pageBlock title="My shop" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Shop Info" columns="2">


<apex:inputField value="{!Shop__c.Account__c}"/>
<apex:inputField value="{!Shop__c.Name}" />
<apex:inputField value="{!Shop__c.Street__c}"/>
<apex:inputField value="{!Shop__c.Street_No1__c}"/>
<apex:inputField value="{!Shop__c.Street_No2__c}"/>
<apex:inputField value="{!Shop__c.Postal_Code__c}"/>
<apex:inputField value="{!Shop__c.City__c}"/> <apex:inputField value="{!Shop__c.Open_Hours_Id__c}"/>

<apex:pageBlockSection title="Shop Info" columns="2">
</apex:pageBlock>
</apex:form>
</apex:page>

I also have a geolocation field called GPS__c however cannot add it to the visualforce pge the same way as those above - <apex:inputField value="{!Shop__c.GPS__c}"/> - but I am getting an error "Unsupported type: common.api.soap.wsdl.Location used in expression: Shop__c.GPS__c" when trying to save it.
What I need is to get geolocation data (latitude, longtitude) filled automatically  when I enter address data like Street, Street_no1, Postal Code etc. and also view the geolocation field, in other words when I enter address details I want to get the longtitude and latitude fields filled automatically in appropriate fields.
I have already managed to get longtitude and latitude data using JSON,please see below..however not sure how to complete the task described above. Really appreciate help on this.
Thanks a lot, Milan 

Getting longtitude and latitude using JSON:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class JSONpokus2 {
    
    String endPointString = 'http://maps.googleapis.com/maps/api/geocode/json?address=Nuselská 23, 140 00, Praha 4';
    double lng;
    double lat;
    
    public double getLng() {
        return lng;
        }
    
    public double getLat() {
        return lat;
        }
    
  

      public void parseJSONResponse() {
          Http httpProtocol = new Http();
          HttpRequest request = new HttpRequest();
          String endpoint = endPointString;
          request.setEndPoint(endpoint);
          request.setMethod('GET');
          HttpResponse response = httpProtocol.send(request); 
          String jsonString = response.getBody();
          googleAddress addr = googleAddress.parse(jsonString);
          
googleAddress.Location loc = addr.firstLoc;
double lng = loc.lng;
double lat = loc.lat;
          
          this.lng=lng;
          this.lat=lat;

      }                      
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Running this code in anonymous window:
-------------------------------------------------------------------------
JSONpokus2 jp2 = new JSONpokus2();
jp2.parseJSONResponse();
system.debug('Geolocation1: '+jp2.getLng());
system.debug('Geolocation2: '+jp2.getLat());
-------------------------------------------------------------------------