2015-08-14 56 views
0

我有一個UIWebView顯示一個只有一個地圖上的網站。我想打開UIWebView並直接到我目前的位置。任何幫助將不勝感激!如何在我的當前位置打開UIWebView?

感謝

下面是我的一些代碼 -

ViewController.h:

#import <UIKit/UIKit.h> 
#import <StoreKit/StoreKit.h> 
#import <CoreMotion/CoreMotion.h> 
#import <CoreLocation/CoreLocation.h> 
#import <MapKit/MapKit.h> 

@interface ViewController : UIViewController <UIWebViewDelegate, MKMapViewDelegate, CLLocationManagerDelegate> 

@property (strong, nonatomic) IBOutlet UIWebView *viewWeb; 
@property (nonatomic, strong) CLLocationManager *locationManager; 
@property (nonatomic, strong) CLLocation *currentLocation; 

@end 

ViewController.m:

#import "ViewController.h" 

@interface ViewController() <UITextViewDelegate> 

@end 

@implementation ViewController 

@synthesize viewWeb; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Request Intel Site 
    NSString *fullURL = @「http://www.websitewithmap.com/」; 
    NSURL *url = [NSURL URLWithString:fullURL]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    viewWeb.scalesPageToFit = YES; 
    viewWeb.scrollView.bounces = NO; 
    [viewWeb loadRequest:requestObj]; 
} 

@end 

我的JavaScript代碼:

// LOCATION HANDLING ///////////////////////////////////////////////// 
    // i.e. setting initial position and storing new position after moving 

    // retrieves current position from map and stores it cookies 
    window.storeMapPosition = function() { 
var m = window.map.getCenter(); 

if(m['lat'] >= -90 && m['lat'] <= 90) 
    writeCookie('gps.intelmap.lat', m['lat']); 

if(m['lng'] >= -180 && m['lng'] <= 180) 
    writeCookie('gps.intelmap.lng', m['lng']); 

writeCookie('gps.intelmap.zoom', window.map.getZoom()); 
    } 


    // either retrieves the last shown position from a cookie, from the 
    // URL or if neither is present, via Geolocation. If that fails, it 
    // returns a map that shows the whole world. 
    window.getPosition = function() { 
if(getURLParam('latE6') && getURLParam('lngE6')) { 
    console.log("mappos: reading email URL params"); 
    var lat = parseInt(getURLParam('latE6'))/1E6 || 0.0; 
    var lng = parseInt(getURLParam('lngE6'))/1E6 || 0.0; 
    var z = parseInt(getURLParam('z')) || 17; 
    return {center: new L.LatLng(lat, lng), zoom: z}; 
} 

if(getURLParam('ll')) { 
    console.log("mappos: reading stock Intel URL params"); 
    var lat = parseFloat(getURLParam('ll').split(",")[0]) || 0.0; 
    var lng = parseFloat(getURLParam('ll').split(",")[1]) || 0.0; 
    var z = parseInt(getURLParam('z')) || 17; 
    return {center: new L.LatLng(lat, lng), zoom: z}; 
    } 

    if(readCookie('gps.intelmap.lat') && readCookie('gps.intelmap.lng')) { 
console.log("mappos: reading cookies"); 
var lat = parseFloat(readCookie('gps.intelmap.lat')) || 0.0; 
var lng = parseFloat(readCookie('gps.intelmap.lng')) || 0.0; 
var z = parseInt(readCookie('gps.intelmap.zoom')) || 17; 

if(lat < -90 || lat > 90) lat = 0.0; 
if(lng < -180 || lng > 180) lng = 0.0; 

return {center: new L.LatLng(lat, lng), zoom: z}; 
    } 

    setTimeout("window.map.locate({setView : true});", 50); 

    return {center: new L.LatLng(0.0, 0.0), zoom: 1}; 
    } 


    ; 
+0

這個帶地圖的網站是否有某種可以通過它的api座標? –

+0

我剛剛對我的問題進行了編輯,顯示我的JavaScript代碼的一部分,我相信該位置被稱爲。我使用 - (void)webViewDidFinishLoad:(UIWebView *)viewWeb NSLog(@「Got here!」); [_viewWeb stringByEvaluatingJavaScriptFromString:@「window.getPosition = function()」]; } – Creagen

+0

我想我現在對你想要做的事感到困惑。您是否無法獲取用戶在設備上的位置,或者您是否無法將該位置的座標傳遞給您的JavaScript,或者JavaScript無法使用傳遞給它的數據? –

回答

1

您需要獲得您的位置,如12.06,53.01。然後你可以使用here.com地圖。要傳遞您的地理位置,您可以轉到here.com/12.06,53.01。或者你可以照常使用MapKit。

相關問題