[英]How to get touch location latitude and longitude value from UIWebView in iPhone?
我正在使用iPhone应用程序,使用UIWebView
加载Google Map地址,并且可以正常工作。 然后,我尝试从UIWebView
获取触摸位置的latitude
和longitude
值,但我不知道如何执行此操作? 是否有可能做到这一点? 请帮我
提前致谢
我尝试了这个:
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];
我为您的触摸事件坐标找到了解决方案,但是您将不得不使用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];
}
我现在测试了此代码,它为我提供了正确的值,还有其他事情总是尝试在设备上而不是模拟器上进行测试。 尝试并回答您的结果,我想现在应该可以解决
根据我上面的评论,如果您将使用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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.