2016-06-07 131 views
1

我已經創建了一個web瀏覽器用它來加載當前用戶的位置。ios - 谷歌地圖在webview中不加載當前位置

在這裏被用於結構:

webview.m - 負載的index.html

的index.html - 顯示數據

的info.plist - NSLocationWhenInUseUsageDescription & NSLocationAlwaysUsageDescription加入

這裏是兩種情況:

1)如果我在Webview中加載Google地圖 - 它d oes沒有加載我的當前位置。紡紗工人正在旋轉並顯示世界地圖。

2)在index.html中,我添加了代碼來加載當前位置的經度和緯度。在筆記本電腦瀏覽器(不是xcode)中,如果我在xcode中打開index.html,它不會顯示我的位置。

可能有些東西需要添加才能讓應用獲取您的位置,例如,在瀏覽器上請求位置權限。在應用程序中,它不問我。

下面是代碼: webview.m

#import "Webview.h" 

@interface Webview() 

@end 

@implementation Webview 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 


    NSString *urlAddress= [[NSBundle mainBundle] pathForResource:@"demos/index" ofType:@"html"]; 
    NSURL *url = [NSURL fileURLWithPath: urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL: url]; 

    [_webView setDelegate:self]; 
    _webView.scrollView.delegate = self; 
    _webView.scrollView.scrollEnabled = true; 
    _webView.scrollView.bounces = NO; 
    [_webView loadRequest:requestObj]; 





} 

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 

} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 



@end 

的index.html

裝載谷歌地圖

<meta http-equiv="refresh" content="0; url=http://maps.google.com/" /> 

加載當前的位置

<p>Address: 
     <div id="address"></div> 
     <div id="l"></div> 
     <div id="lo"></div> 
    </p> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 

    var currgeocoder; 

    //Set geo location lat and long 
    navigator.geolocation.getCurrentPosition(function (position, html5Error) { 
     geo_loc = processGeolocationResult(position); 
     currLatLong = geo_loc.split(","); 
     initializeCurrent(currLatLong[0], currLatLong[1]); 
    }); 

    //Get geo location result 
    function processGeolocationResult(position) { 
     html5Lat = position.coords.latitude; //Get latitude 
     html5Lon = position.coords.longitude; //Get longitude 
     html5TimeStamp = position.timestamp; //Get timestamp 
     html5Accuracy = position.coords.accuracy; //Get accuracy in meters 
     return (html5Lat).toFixed(8) + ", " + (html5Lon).toFixed(8); 
    } 

    //Check value is present or 
    function initializeCurrent(latcurr, longcurr) { 
     currgeocoder = new google.maps.Geocoder(); 

     console.log(latcurr + "-- ######## --" + longcurr); 

     if (latcurr != '' && longcurr != '') { 
      //call google api function 
      var myLatlng = new google.maps.LatLng(latcurr, longcurr); 
      return getCurrentAddress(myLatlng); 
     } 
    } 

    //Get current address 
    function getCurrentAddress(location) { 
     currgeocoder.geocode({ 
      'location': location 
     }, function (results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       console.log(results[0]); 
       $("#address").html(results[0].formatted_address); 
       $("#l").html(html5Lat); 
       $("#lo").html(html5Lon); 

      } else { 
       alert('Geocode was not successful for the following reason: ' + status); 
      } 
     }); 
    } 
}); 
    </script> 

info.plist

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>CFBundleDevelopmentRegion</key> 
    <string>en</string> 
    <key>CFBundleExecutable</key> 
    <string>$(EXECUTABLE_NAME)</string> 
    <key>CFBundleIcons</key> 
    <dict/> 
    <key>CFBundleIcons~ipad</key> 
    <dict/> 
    <key>CFBundleIdentifier</key> 
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> 
    <key>CFBundleInfoDictionaryVersion</key> 
    <string>6.0</string> 
    <key>CFBundleName</key> 
    <string>$(PRODUCT_NAME)</string> 
    <key>CFBundlePackageType</key> 
    <string>APPL</string> 
    <key>CFBundleShortVersionString</key> 
    <string>1.0</string> 
    <key>CFBundleSignature</key> 
    <string>????</string> 
    <key>CFBundleVersion</key> 
    <string>1</string> 
    <key>NSLocationWhenInUseUsageDescription </key> 
    <string>Give us permission to use your location</string> 
    <key>NSLocationAlwaysUsageDescription</key> 
    <string>Your message goes here</string> 
    <key>LSRequiresIPhoneOS</key> 
    <true/> 
    <key>UILaunchStoryboardName</key> 
    <string>Main</string> 
    <key>UIMainStoryboardFile</key> 
    <string>Main</string> 
    <key>UIRequiredDeviceCapabilities</key> 
    <array> 
     <string>armv7</string> 
    </array> 
    <key>UISupportedInterfaceOrientations</key> 
    <array> 
     <string>UIInterfaceOrientationPortrait</string> 
     <string>UIInterfaceOrientationLandscapeLeft</string> 
     <string>UIInterfaceOrientationLandscapeRight</string> 
    </array> 
    <key>NSAppTransportSecurity</key> 
    <dict> 
     <key>NSAllowsArbitraryLoads</key> 
     <true/> 
     <key>NSExceptionDomains</key> 
     <dict> 
      <key>akamaihd.net</key> 
      <dict> 
       <key>NSIncludesSubdomains</key> 
       <true/> 
       <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
       <false/> 
      </dict> 
      <key>facebook.com</key> 
      <dict> 
       <key>NSIncludesSubdomains</key> 
       <true/> 
       <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
       <false/> 
      </dict> 
      <key>fbcdn.net</key> 
      <dict> 
       <key>NSIncludesSubdomains</key> 
       <true/> 
       <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
       <false/> 
      </dict> 
     </dict> 
    </dict> 
</dict> 
</plist> 

問題:如何啓用我的應用程序來請求位置權限?或直接加載位置?

回答

0

問題:如何使我的應用程序能夠請求位置許可?或直接加載位置?

首先,這裏有一個SO thread,它可能提供使用UIWebView顯示地圖的解決方案。

嘗試使用locationManager(_:didChangeAuthorizationStatus:)啓用這個權限:

關於權限在Google地圖iOS版

現在。這意味着您在使用應用程序時給予用戶權限。

extension MapViewController: CLLocationManagerDelegate { 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 

    if status == .AuthorizedWhenInUse { 

     locationManager.startUpdatingLocation() 
     mapView.myLocationEnabled = true 
     mapView.settings.myLocationButton = true 
    } 
    } 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let location = locations.first { 

     mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) 

    locationManager.stopUpdatingLocation() 
    } 

    } 
} 

欲瞭解完整的教程親切check this

相關問題