2012-11-19 85 views
1

我正在使用iPhone應用程序,使用UIWebView加載Google地址及其工作正常。 然後,我試圖從UIWebView獲得觸摸位置latitudelongitude的值,但我不知道,如何做到這一點?是否有可能做到這一點?請幫我如何從iPhone中的UIWebView獲取觸摸位置的緯度和經度值?

由於提前

我嘗試這樣做:

webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 40, 320,376)]; 
    webView.delegate=self; 
    webView.scalesPageToFit=YES; 
    NSLog(@"URL:%@",[user valueForKey:@"google_Address"]); 
    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=37.785834,-122.406417&output=embed"]; 
    NSURL *url=[NSURL URLWithString:googleMapsURLString]; 
    NSURLRequest *requestObj=[NSURLRequest requestWithURL:url]; 
    [webView loadRequest:requestObj]; 
    [self.view addSubview:webView]; 
+0

使用MapKit而不是webview來打開地圖。 – spider1983

回答

1

我得到了你的觸摸事件聯合解決方案座標,但你將不得不使用的MKMapView我正在粘貼下面

的代碼在.H

#import <MapKit/MapKit.h> //import this in your project 

//define the objects; 
MKMapView *mapView; 
CLLocationCoordinate2D coordinate; 

不要忘記添加MapKit.framework在您的項目

在.M

- (void)viewDidLoad 
{ 

    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];//Release it in dealloc 
    mapView.delegate=self; 

    [self.view addSubview:mapView]; 
    [self displayRegion]; 

    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]initWithTarget:self  
    action:@selector(handleGesture:)]; 
    tgr.numberOfTapsRequired = 1; 
    [mapView addGestureRecognizer:tgr]; 
    [tgr release]; 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    } 

    - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer 
    { 
     if (gestureRecognizer.state != UIGestureRecognizerStateEnded) 
      return; 

     CGPoint touchPoint = [gestureRecognizer locationInView:mapView]; 
     coordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; 

      NSLog(@"latitude %f longitude %f",coordinate.latitude,coordinate.longitude); 


     } 


     -(void)displayRegion 
     { 
      MKCoordinateRegion region; 
      MKCoordinateSpan span; 
      span.latitudeDelta=0.2; 
      span.longitudeDelta=0.2; 

      CLLocationCoordinate2D location; 

      location.latitude = 22.569722 ; 
      location.longitude = 88.369722; 


      region.span=span; 
      region.center=location; 

      [mapView setRegion:region animated:TRUE]; 
      [mapView regionThatFits:region]; 
      } 

我測試了這個代碼,現在它給了我正確的值,還有一件事總是試圖在不在模擬器上的設備上進行測試。嘗試並回復你的結果,我猜它應該現在做的伎倆

+0

謝謝我現在檢查並更新asap – SampathKumar

+0

您可以創建一個新項目並複製粘貼上面的代碼來檢查其工作與否,然後嘗試在您的原始項目中實現它,您將會了解許多事情。 – spider1983

0

按我上面的評論,如果你會使用MapKit然後你可以用下面的代碼來獲取經/緯的觸摸位置

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ 
CLLocationCoordinate2D coord= [MyMapView convertPoint:point toCoordinateFromView:MyMapView]; 
NSLog(@"latitude %f longitude %f",coord.latitude,coord.longitude); 


return [super hitTest:point withEvent:event]; 

}

+0

感謝您的回覆 – SampathKumar

+0

我會檢查它更新ü – SampathKumar

+0

你可以給這個任何教程? – SampathKumar

相關問題