1

我在 iPhone 应用程序中工作,UIWebView用于加载谷歌地图地址,它工作正常。然后我试图从中获取触摸位置latitudelongitudeUIWebView,但我不知道,如何做到这一点?是否有可能做到这一点?请帮我

提前致谢

我试过这个:

    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];  
4

2 回答 2

1

我得到了你的触摸事件坐标的解决方案,但你必须使用 MKmapView 我粘贴下面的代码

在.h

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

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

不要忘记在你的项目中添加 MapKit.framework

- (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];
          }

我现在测试了这段代码,它给了我正确的值,还有一件事总是试图在设备上而不是在模拟器上测试它。尝试并回复您的结果,我想它现在应该可以解决问题

于 2012-11-19T11:36:46.380 回答
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];

}

于 2012-11-19T07:27:07.643 回答