将从应用程序获取的地理位置传递给 UIWebView

Passing geolocation fetched from the app to the UIWebView(将从应用程序获取的地理位置传递给 UIWebView)
本文介绍了将从应用程序获取的地理位置传递给 UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 iOS 应用程序,我想在其中一个视图中包含 UIWebView 和包含地图的页面(地址是:https://carsharing.mvg-mobil.de).该网站需要位置许可,因此一旦用户看到此视图,就会出现一条警报:https://carsharing.mvg-mobil.de/ 想使用您当前的位置.OK/不允许".

I'm developing the iOS app and in one of the views I would like to include UIWebView with a page that contains a map (the address is: https://carsharing.mvg-mobil.de). The website requires location permission so as soon as the user is presented with this view, an alert appears: "https://carsharing.mvg-mobil.de/ would like to use your current location. OK/Don't allow".

在我的应用程序中,我从 CoreLocation 框架中获取了用户的位置.有没有办法将此位置传递给此 web 视图以避免此警报视图?是否可以通过注入一些 JavaScript 代码或者以某种方式在 URL 中包含地理位置?

In my app I have the location of the user fetched from the CoreLocation framework. Is there a way to pass this location to this webview to avoid this alert view? Would it be somehow possible by injecting some JavaScript code or maybe somehow including geolocation in the URL?

推荐答案

好的,我在PhoneGap 如何使用从 CoreLocation 获取的位置将 JavaScript 代码注入标准 webview:

OK, I found the solution in the source code of PhoneGap how to inject JavaScript code to a standrad webview with the location fetched from CoreLocation:

int epoch = [newLocation.timestamp timeIntervalSince1970];
float course = -1.0f;
float speed  = -1.0f;
NSString* coords =  [NSString stringWithFormat:@"coords: { latitude: %f, longitude: %f, altitude: %f, heading: %f, speed: %f, accuracy: %f, altitudeAccuracy: %f }",
                        newLocation.coordinate.latitude,
                        newLocation.coordinate.longitude,
                        newLocation.altitude,
                        course,
                        speed,
                        newLocation.horizontalAccuracy,
                        newLocation.verticalAccuracy
                     ];

NSString * jsCallBack = [NSString stringWithFormat:@"navigator.geolocation.setLocation({ timestamp: %d, %@ });", epoch, coords];

[webView stringByEvaluatingJavaScriptFromString:jsCallBack];

希望其他人也可以从这段代码中获利.

Hopefully somebody else can profit from this code as well.

这篇关于将从应用程序获取的地理位置传递给 UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)