2016-06-07 21 views
0

Google爲GoogleMap提供了an iOS SDK,但是,GoogleMap JavaScript API提供了更多功能,包括custom stylingdata visualisation,這些功能都是我想要在我的應用中實現的功能。如何在iOS應用程序中使用GoogleMap JavaScript API?

那麼有什麼方法可以在我的應用程序中使用這些JavaScript API?

我在想我是否可以在WebView中嵌入地圖,以便我可以在應用程序中執行JavaScript內容?這可能嗎?績效如何危害?

在此先感謝。

回答

1

我正在使用AFNetworking框架。我做了它的子類。 CUK_Client是子類的名稱。

google api examples顯示它們的方法類型(POST或GET)。

您可以根據自己的需要進行修改。

makeRequestGet是一種通用的GET方法,您可以根據需要使用任何參數和路由對其進行調用。例如,在getDirection方法中,我使用它來獲取兩個座標之間的方向。

.h文件中

@interface CUK_Client : AFHTTPSessionManager 
+ (CUK_Client *) sharedClient; 
+ (CUK_Client *) googleMapsClient; 

- (void)makeRequestGet:(NSString *) route withParamaters:(NSDictionary *)parameters completion:(DefaultIdResultBlock) completion ; 

- (void)getDirection:(NSDictionary *)params completion:(DefaultIdResultBlock) completion; 

- (void)beaconsVisited:(NSArray *)beacons completion:(DefaultIdResultBlock) completion; 
@end 

.m文件

@implementation CUK_Client 

+ (CUK_Client *) googleMapsClient { 
static CUK_Client *googleMapsClient = nil; 
static dispatch_once_t onceToken; 
dispatch_once (&onceToken ,^{ 
    googleMapsClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]]; 

}); 

return googleMapsClient; 
} 


    - (void) makeRequestGet:(NSString *) route withParamaters:(NSDictionary *)parameters completion:(DefaultIdResultBlock) completion { 

    [self GET:route 
    parameters:parameters 
     success:^(NSURLSessionDataTask *task, id responseObject) { 

      return completion (nil ,responseObject); 

     } failure:^(NSURLSessionDataTask *task, NSError *error) { 

      return completion ([error localizedDescription] , nil); 
     }]; 
} 

-(void)getDirection:(NSDictionary *)params completion:(DefaultIdResultBlock)completion { 

     [self makeRequestGet:@"maps/api/directions/json" withParamaters:params completion:completion]; 
    } 

@end 
+0

@Kivannc你好,謝謝你的回答。但是,我不只是向Web服務API發出HTTP請求,但實際上我需要直接執行JavaScript。所以我現在正在考慮使用UIWebiview的 - stringByEvaluatingJavaScriptFromString:這樣做。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString: –

相關問題