PeopleCode to retrieve Google map between two addresses

 

PeopleCode Example:

/* Define constants for the API request */

Local string &origin = "123 Main St, Anytown, USA";  /* Replace with the starting address */

Local string &destination = "456 Elm St, Othertown, USA";  /* Replace with the destination address */

Local string &apiKey = "YOUR_GOOGLE_MAPS_API_KEY";  /* Replace with your Google Maps API Key */

 

 

/* Construct the Google Maps API URL */

Local string &baseUrl = "https://maps.googleapis.com/maps/api/directions/json?";

Local string &url = &baseUrl | "origin=" | %EncodeURL(&origin) | "&destination=" | %EncodeURL(&destination) | "&key=" | &apiKey;

 

/* Create an HTTP request object */

Local object &http = CreateObject("HttpClient");

Local integer &status;

 

/* Send the HTTP GET request */

&status = &http.Get(&url);

 

/* Check if the request was successful */

If &status <> 0 Then

   /* Handle error */

   MessageBox(0, "", 0, 0, "Error sending request to Google Maps API: " | &http.GetLastError());

Else

   /* Parse the JSON response */

   Local string &response = &http.GetResponseText();

   Local object &jsonResponse = JSON.Parse(&response);

  

   /* Check if the response contains routes */

   If All(&jsonResponse.Routes) Then

      Local object &route = &jsonResponse.Routes[1];

     

      /* Retrieve route details */

      Local string &summary = &route.summary;

      Local string &distance = &route.legs[1].distance.text;

      Local string &duration = &route.legs[1].duration.text;

     

      /* Display the results */

      MessageBox(0, "", 0, 0, "Route: " | &summary | Char(13) | "Distance: " | &distance | Char(13) | "Duration: " | &duration);

   Else

      /* Handle no routes found */

      MessageBox(0, "", 0, 0, "No routes found between the specified addresses.");

   End-If;

End-If;

 

Example-2: Here you can use two strings as input parameter from page

Local string  &Fromaddr,&Toaddr;

&Fromaddr = SY_MAPS_INT_WRK.SY_FROM_LOCATION.Value;
&Toaddr = SY_MAPS_INT_WRK.SY_TO_LOCATION.Value;

&Map_Selection = “http://maps.google.com/maps?saddr=%FromAddr%&daddr=%ToAddr%&#8221;;
&Map_Selection = Substitute(&Map_Selection, “%FromAddr%”, &Fromaddr);
&Map_Selection = Substitute(&Map_Selection, “%ToAddr%”, &Toaddr);
ViewContentURL(&Map_Selection, True);


Advanced Integration Steps

Handling Different Travel Modes

Google Maps Directions API supports multiple travel modes such as driving, walking, bicycling, and transit. You can modify the request URL to specify a travel mode.

/* Define the travel mode */

Local string &travelMode = "driving";  /* Options: driving, walking, bicycling, transit */

 

/* Construct the Google Maps API URL with travel mode */

Local string &url = &baseUrl | "origin=" | %EncodeURL(&origin) | "&destination=" | %EncodeURL(&destination) | "&mode=" | &travelMode | "&key=" | &apiKey;

 

Optimizing API Requests

Waypoints: If you want to include intermediate stops in your route, you can add waypoints.

Local string &waypoints = "via:123+Main+St,Anytown,USA|via:789+Oak+St,Newtown,USA";  /* Example waypoints */

Local string &url = &baseUrl | "origin=" | %EncodeURL(&origin) | "&destination=" | %EncodeURL(&destination) | "&waypoints=" | &waypoints | "&key=" | &apiKey;

 

Departure Time: For transit mode, you can specify a departure time to get more accurate directions.

Local string &departureTime = "now";  /* You can also use a specific timestamp */

Local string &url = &baseUrl | "origin=" | %EncodeURL(&origin) | "&destination=" | %EncodeURL(&destination) | "&mode=" | &travelMode | "&departure_time=" | &departureTime | "&key=" | &apiKey;

 

Enhanced Error Handling

Implement more comprehensive error handling to deal with different types of errors, including network issues, invalid API responses, and more.

If &status <> 0 Then

   /* Handle HTTP errors */

   MessageBox(0, "", 0, 0, "HTTP Request Error: " | &http.GetLastError());

Else

   /* Parse the JSON response and check for API-specific errors */

   Local object &jsonResponse = JSON.Parse(&http.GetResponseText());

 

   If All(&jsonResponse.Status = "OK") Then

      Local object &route = &jsonResponse.Routes[1];

      /* Proceed with parsing and displaying route information */

   Else

      /* Handle API-specific errors */

      Local string &errorMessage = &jsonResponse.Error_message;

      MessageBox(0, "", 0, 0, "Google Maps API Error: " | &errorMessage);

   End-If;

End-If;

Data Parsing and Utilization

You can extract additional details from the API response, such as steps in the route, estimated arrival times, and more.

 

Local object &leg = &route.legs[1];

Local array of object &steps = &leg.steps;

 

For &i = 1 To &steps.Len

   Local object &step = &steps[&i];

   Local string &instruction = &step.html_instructions;

   Local string &stepDistance = &step.distance.text;

   Local string &stepDuration = &step.duration.text;

 

   /* Display or process each step in the journey */

   MessageBox(0, "", 0, 0, "Step " | &i | ": " | &instruction | " (" | &stepDistance | ", " | &stepDuration | ")");

End-For;

 

Security Enhancements

  • Secure API Key Storage: Store the API key securely within PeopleSoft, using configuration or encrypted storage options to prevent exposure in the codebase.
  • Use of Environment Variables: Instead of hardcoding the API key, use environment variables or secure PeopleSoft configuration values to inject the key at runtime.

User Interface Integration

If you want to display the map within a PeopleSoft page, you can embed a Google Maps iframe using the map URL. Alternatively, you can use JavaScript on a PeopleSoft page to display dynamic map content.

 

 

PeopleCode to retrieve Google map between two addresses

  PeopleCode Example: /* Define constants for the API request */ Local string &origin = "123 Main St, Anytown, USA";   /* ...