gpt4 book ai didi

android - 在 TWebBrowser 中将图钉添加到 Google map 并处理点击

转载 作者:可可西里 更新时间:2023-11-01 04:02:38 24 4
gpt4 key购买 nike

如何在 TWebBrowser 中将图钉添加到 Google map 并在图钉被点击时调用事件?该解决方案需要同时适用于 iOS 和 Android。

最佳答案

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

在 iOS 上,它是通过调用一个名为 stringByEvaluatingJavaScriptFromString 的 Web View 方法来完成的:

[webview stringByEvaluatingJavaScriptFromString:@"addPin()"];

要在 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;

它会在 native 端触发 web View 的 shouldStartLoadWithRequest: 委托(delegate),因此您所要做的就是读取请求并处理...

- (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;

}

这个例子的一部分来自 here

希望对您有所帮助!

关于android - 在 TWebBrowser 中将图钉添加到 Google map 并处理点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18779164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com