Invoking JavaScript to Compute Driving Distance in a iOS App

Need to compute driving distance by calling JavaScript from an iOS app. Revised HTML/JS and Objective-C snippets follow. Is the maps API reference or another issue causing failure?

<html>
  <head>
    <script type="text/javascript">
      var pathHandler;
      function initMap() {
          pathHandler = {};
      }
      function computeDistance(fromLoc, toLoc) {
          // Dummy calculation
          return 25;
      }
    </script>
  </head>
  <body onload="initMap()">
    <input type="text" id="fromLoc" />
    <input type="text" id="toLoc" />
    <button onclick="alert(computeDistance(document.getElementById('fromLoc').value, document.getElementById('toLoc').value))">Get Distance</button>
  </body>
</html>
NSString *jsCall = [NSString stringWithFormat:@"computeDistance('%@','%@')", startPlace, endPlace];
NSString *distanceResult = [webView evaluateJavaScript:jsCall];
self.distanceLabel.text = distanceResult;

I encountered a similar situation recently when trying to integrate JavaScript functions with my iOS application. Initially, I assumed it was an issue with the mapping API reference, but it turned out to be related to how the JavaScript was being called. In my case, the problem was that the function call did not account for asynchronous execution in certain environments. I found that explicitly checking the return value and ensuring that the web view was fully loaded before invoking the function helped to reveal the actual timing issues. Reviewing the order of execution and making sure the JavaScript environment was ready often resolved the issue.

In my experience, the issue was not really in the map API reference but more in the asynchronous handling of JavaScript execution in the WebView. I experienced similar problems where the JavaScript function did not return the expected value because it was running before the page was fully ready or fully loaded. The remedy for me was to ensure that the WebView’s delegate confirmed the page load completion before the call. Additionally, I found logging and debugging the call to identify timing problems particularly useful.