Passing geolocation fetched from the app to the 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将从应用程序获取的地理位置传递给 UIWebView
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01