在TWebBrowser中将引脚添加到Google Map并处理点击

nor*_*aul 6 delphi android google-maps ios delphi-xe5

如何在TWebBrowser中将引脚添加到Google Map并在单击引脚时调用事件?该解决方案需要适用于iOS和Android.

For*_*nja 0

您必须在本机(iOS/Android)和 GoogleMaps javascript 代码之间建立一些桥梁。例如,要在地图上放置图钉,您必须在 js 端调用 addPin 方法注入 js 代码。

在 iOS 上,这是通过调用名为 stringByEvaluatingJavaScriptFromString 的 Web 视图方法来完成的:

[webview stringByEvaluatingJavaScriptFromString:@"addPin()"];
Run Code Online (Sandbox Code Playgroud)

要监听 webview 上的点击事件,您必须在点击 pin 时实现回调,并使用 iFrame 方法调用 objC 方法:

var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", "js-frame:myPinTappedMethod";
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
Run Code Online (Sandbox Code Playgroud)

它将在本机端触发Web视图的shouldStartLoadWithRequest:委托,因此您所要做的就是读取请求并处理...

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{

  // Intercept custom location change, URL begins with "js-call:"
  if ([[[request URL] absoluteString] hasPrefix:@"js-call:"]) {

    // Extract the selector name from the URL
    NSArray *components = [requestString componentsSeparatedByString:@":"];
    NSString * functionName = [components objectAtIndex:1];

    // Call the given selector
    [self performSelector:NSSelectorFromString(functionName)];

    // Cancel the location change
    return NO;
  }

  // Accept this location change
  return YES;

}
Run Code Online (Sandbox Code Playgroud)

这个例子的一部分是我从这里获取的

希望能帮助到你!