问题
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
Thanks in Advance
I tried this:
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];
回答1:
i got the solution for your touch event coordinates but you will have to use MKmapView i am pasting the codes below
in .h
#import <MapKit/MapKit.h> //import this in your project
//define the objects;
MKMapView *mapView;
CLLocationCoordinate2D coordinate;
don't forget to add MapKit.framework in your project
in .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
回答2:
As per my above comment if you will use MapKit Then you can use the following code to get the lat/long of the touch location
- (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];
}
来源:https://stackoverflow.com/questions/13449184/how-to-get-touch-location-latitude-and-longitude-value-from-uiwebview-in-iphone