将从应用程序获取的地理位置传递给 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

AWS DynamoDB Batch Get Request - iOS(AWS DynamoDB 批量获取请求 - iOS)
Querying DynamoDB on non-key attributes(在非关键属性上查询 DynamoDB)
DynamoDB auto incremented ID amp; server time (iOS SDK)(DynamoDB 自动递增 ID amp;服务器时间(iOS SDK))
Where to find a clear explanation about swift alert (UIAlertController)?(哪里可以找到关于 swift alert (UIAlertController) 的清晰解释?)
Facebook Requests Dialog: Frictionless Requests in native iOS app possible?(Facebook 请求对话框:本机 iOS 应用程序中的无摩擦请求可能吗?)
dynamodb scanexpression with scan filter in objective-c(在objective-c中带有扫描过滤器的dynamodb scanexpression)