Get touch position google maps iOS - ios

GMSMapView creates a UIView that handles gestures recognizer, I want detect where in the UIView the user is giving a touch, I can get the marker position (relative to map coordinates) but I want the current position of the touch on the view. Any ideas?.

If I got the question right, you want to calculate CGPoint of touch location in Map's UIView?
So, you have a CLLocationCoordinate2D you got from Delegate's method, you can get it's position by calling on GMSProjection object on you GMSMapview like:
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
CGPoint locationInView = [mapView.projection pointForCoordinate:coordinate]; // capitalize the V
}
and the locationInView will be the desired location. So, the GMSProjection's method
- (CGPoint)pointForCoordinate:(CLLocationCoordinate2D)coordinate;
gives you point of touch in Map's view.
Hope it helps :)

Related

Correct touch location after zooming and panning a pdf drawn in CATiledLayer on top of the UIScrollview in iOS

I am working on Atlas App in which I am displaying map which I can zoom and pan using pdf file. I am using vfr reader for this purpose and it is working fine. I want to detect the touch location so that I can get the correct state selected. I am getting the correct coordinate when view is not zoomed and panned using the code below:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:theScrollView];
}
But,when I zoom it out and pan it,the touch location changes and I am not getting the correct state selected. How will I get the correct selected state?
On debugging vfr reader classes I found that I can get the correct exact location of touch in ReaderContentPage class. This class gives the correct touch location after zooming also. You can get the point in processingSingleTap method as below:
- (id)processSingleTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self];
}
CGPoint point gives the correct touch location. And then use the delegate method to get the correct coordinates in the required class.

Cocos2d Return touch location on a sprite's body

How I can get the touch location on a sprite body so I know what force to apply to the body.
So for instance if the sprite and body is in the shape of a rectangle and the player touches the top half of the rectangle, I'd want to apply a downward force or if the player touches the rectangle in the middle of the shape then I don't want to apply any force.
I can work out how to check if the touch location of the player's finger is on the object but calculating where the player touched on the object's body itself, I'm not sure how to go about it.
It would be better if you shown your code, but generally you can get touch location inside the sprite either using locationInNode: or convertToNodeSpace: methods, depending on what you already have.
Something like this:
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
//Check if you touched the sprite in case
//you’re handling touches in your scene
//If you subclassed CCSprite and handling touches there
//you don’t have to do anything here.
CCSprite *yourSprite = ...;
CGPoint touchLocation = [touch locationInNode:yourSprite];
float halfHeight = yourSprite.boundingBox.size.height * 0.5f;
if (touchLocation.y >= halfHeight)
{
//Upper part
}
else
{
//lower part
}
}
Note, that you can handle touches in the scene, and then you first need to check whether you touched your sprite at all, or you can subclass CCSprite and implement touchBegan: method to get notified when the player touches your sprite (then you don’t need to check anything and yourSprite becomes self).

Is there a way to fix the centre point of a MKMapView, specifically when the user is changing the zoom level?

I'm trying to have a map view centre on one point so the user can smoothly zoom in and out but remain centred on that point.
I've a less than optimal solution by centring the map when regionDidChangeAnimated is called, plus a flag to stop the code looping infinitely...
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
if (!self.isRecentring) {
self.isRecentring = YES;
[mapView setCenterCoordinate:self.centreLocation animated:YES];
self.isRecentring = NO;
}
}
So, that works but only does its thing once the user has finished changing the zoom, meaning there's a quick scroll to reposition the map afterward.
I've tried the equivalent with regionWillChangeAnimated but that just kills the zoom dead, I'm presuming because my setCentreCoordinate sets a new region and ends the zoom gesture?
Any ideas how I can work around this and maintain the centre point mid-zoom?
I know this is old, but I came up with a solution that doesn't appear to have any rendering issues.
Set scrollEnabled, zoomEnabled, and rotateEnabled to false on the mapView
Add a UIPinchGestureRecognizer to the map view
In the pinch gesture recognizer handler, save the region at the start of the gesture. You can use recognizer.state == .began to detect this.
In the pinch gesture recognizer handler, when recognizer.state == .changed, multiply the start region's span.longitudeDelta and span.latitudeDelta by recognizer.scale and perform a mapView.setRegion with no animation.

MKMapView centerLocation

In my application there is an MKMapView and I am trying to get the center coordinates of the map region that is currently visible. I am using following method so that if user moves the visible region I'll get new center coordinates.
- (void)mapView:(MKMapView *)mapView1 regionDidChangeAnimated:(BOOL)animated
{
CLLocationCoordinate2D centre = [mapView centerCoordinate];
NSLog(#"MAP CENTER = %f,%f",centre.latitude,centre.longitude);
}
the problem is that when I switch to the UIViewController that contains MKMapView it gives MAP CENTER = 0.000000,0.000000 for two times then gives the actual coordinates MAP CENTER = 55.755786,37.617633. I want the actual coordinates as soon as I switch to that UIViewController.
Is the coordinates (55.755786,37.617633) your current location ?
MKMapView takes some time to get a lock on GPS to fetch the coordinates for your current location. Until then centerCoordinate might return (0,0)
Try this this may help you.
self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;

How do I get the tap coordinates on a custom UIButton?

I'm using XCode 4.4 developing for iOS 5 on an iPad and am using the Storyboard layout when creating my custom button.
I have the touch event correctly working and logging but now I want to get the x/y coordinates of the tap on my custom button.
If possible, I'd like the coordinates to be relative to the custom button instead of relative to the entire iPad screen.
Here's my code in the .h file:
- (IBAction)getButtonClick:(id)sender;
and my code in the .m file:
- (IBAction)getButtonClick:(id)sender {
NSLog(#"Image Clicked.");
}
Like I said, that correctly logs when I tap the image.
How can I get the coordinates of the tap?
I've tried a few different examples from the internet but they always freeze when it displays a bunch of numbers (maybe the coordinates) in the log box. I'm VERY new to iOS developing so please make it as simple as possible. Thanks!
To get touch location you can use another variant of button action method: myAction:forEvent: (if you create it from IB interface note "sender and event" option in arguments field: )
Then in your action handler you can get touch location from event parameter, for example:
- (IBAction)myAction:(UIButton *)sender forEvent:(UIEvent *)event {
NSSet *touches = [event touchesForView:sender];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:sender];
NSLog(#"%#", NSStringFromCGPoint(touchPoint));
}
Incase of Swift 3.0 the accepted answer works same except syntax will be changed as follows:
Swift 3.0:
#IBAction func buyTap(_ sender: Any, forEvent event: UIEvent) {
let myButton = sender as! UIButton
let touches = event.touches(for: myButton)
let touch = touches?.first
let touchPoint = touch?.location(in: myButton)
print("touchPoint\(touchPoint)")
}
For your overall coordinates (with reference to the screen), you need to create a CGPoint that contains the coordinates of your touch. But to do that, you need to get that touch first. So start by getting the touch event, then by making that point using the locationInViewmethod. Now, depending on when you want to log the touch - when the user touches down, or when they lift their finger -, you have to implement this code in the touchesBegan or touchesEnded method. Let's say you do touchesEnded, which passes an NSSet cales "touches" containing all the touch events.
UITouch *tap = [touches anyObject];
CGPoint touchPoint = [tap locationInView:self.view];
"touchPoint" will now contain the point at which the user lifts their finger. To print out the coordinates, you just access the x and y properties of that point:
CGFloat pointX = touchPoint.x;
CGFloat pointY = touchPoint.y;
NSLog(#" Coordinates are: %f, %f ", pointX, pointY);
That should output the coordinates of the touch. Now to have it be referenced to whatever button you're using, I would suggest you just manually subtract the values for the button's coordinates from the point. It seems like a simple solution, and honestly I don't know a way of getting coordinates with reference to another object, unless you make a view based on that object, and pass it to locationInView instead of self.view.
For more info on touches, there's a great set of tutorials here.

Resources