如何从iPhone中的UIWebView获取触摸位置的纬度和经度值? [英] How to get touch location latitude and longitude value from UIWebView in iPhone?
问题描述
我正在使用iPhone应用程序,使用UIWebView
加载Google Map地址,并且可以正常工作.
然后我尝试从UIWebView
获取触摸位置latitude
和longitude
值,但是我不知道如何执行此操作?是否有可能做到这一点?请帮助我
I'm working in iPhone application, Using UIWebView
to load Google Map address and its works fine.
Then i tried to get the touch location latitude
and longitude
value from UIWebView
, but i didn't know that, How to do this? Is it possible to do this? please help me
预先感谢
我尝试过:
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粘贴以下代码
i got the solution for your touch event coordinates but you will have to use MKmapView i am pasting the codes below
在.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];
}
我现在测试了这段代码,它为我提供了正确的值,还有另一件事总是尝试在设备上而不是模拟器上进行测试.尝试并回答您的结果,我想它应该可以解决问题
i tested this code now its giving me correct values, and one more thing always try to test this on device not on simulator. Try and reply with your result i guess it should do the trick now
这篇关于如何从iPhone中的UIWebView获取触摸位置的纬度和经度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!