Passing geolocation fetched from the app to the UIWebView

后端 未结 2 1440
情话喂你
情话喂你 2021-01-07 10:11

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

2条回答
  •  孤城傲影
    2021-01-07 10:48

    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.

提交回复
热议问题