I'm using Swift 3 and I want to create a moving marker in my map view, so that if the user touches the screen and moved the marker also moves with the page.
How can I fix this?
Use GMSMapViewDelegate method :
mapView:didChangeCameraPosition:
Set GMSMarker in this method at camera position.
This method call when camera position change in map.
func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
// Apply set marker logic here
}
Related
I have a MKMapview with lot of polygons added as overlays, To optimize the memory I need to know before adding an overlay if the polygon is inside visible area of the MKMapview
I can even create a whole visible area polygon with mapview all corner coordinates, for example topLeft as below
func topLeftCoordinate() -> CLLocationCoordinate2D {
return convert(bounds.origin, toCoordinateFrom: self)
}
with all the corner coordinates, I can create a current_visible_area_polygon and I want to check the polygons I add is inside this current_visible_area_polygon.
so it comes down to two questions
Is it possible to check if a polygon is inside another polygon or atleast intersects OR
if a polygon is inside visible maprect
I found the answer to be the following
let mapView: MKMapView
let mkPolygon: MKPolygon
mapView.visibleMapRect.isIntersects(mkPolygon)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am developing an app where I am using Google maps.
I want to have a marker pin to be steady in the centre of screen and if I move the map the pin should not be moved.
When I leave the map, I want the address from the map where the steady marker id kept.
Any help will be appreciated
You can use this things
mapView.delegate = self;
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
let coordinate = mapView.projection.coordinate(for: markerView.center)
print("latitude " + "\(coordinate.latitude)" + " longitude " + "\(coordinate.longitude)")
}
Now you need to add just a imageview on top of your mapview in the center and it will work same as uber.
Remember the height of pin icon should be doubled.
You can use this icon if you need
You could add a image layer above the map view, containing the image of the pin pointing to the center of the screen. When the user leaves the map pick the location from the center of the screen.
Fix your marker view on the center in your view.
Add GMSMapview in your top view.
Set Delegate to your map view.
GMap.delegate = self; // Add delegate - GMSMapViewDelegate to your viewcontroller
To detect if the user dragged the map:
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
let coo = GMap.projection.coordinate(for: markerView.center)
selectedLocation = CLLocation(latitude: coo.latitude, longitude: coo.longitude)
getAddress() //get address of your selected location
}
Is it possible to change a GMSPolygon .fillColor property from the didTapOverlay GMSMapViewDelegate delegation method? This is the delegation method:
func mapView(mapView: GMSMapView, didTapOverlay overlay: GMSOverlay)
my problem is that GMSPolygon inherits from GMSOverlay and the overlay doesn't have a fillColor property. The result I would like to achieve is to change the color of the polygon when the user taps it
Thanks
I met the same situation before (I used Objective-C for that time).
My solution was that generating unique IDs(random strings or number), then set them to the title property of each GMSPolygons.
The below code is written by Objective-C, but you might understand
- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay {
NSString *overlayClass = NSStringFromClass([overlay class]);
if ([overlayClass isEqualToString:#"GMSPolygon"] ||
[overlayClass isEqualToString:#"GMSPolyline"] ||
[overlayClass isEqualToString:#"GMSCircle"] ||
[overlayClass isEqualToString:#"GMSGroundOverlay"]) {
[self triggerOverlayEvent:#"overlay_click" id:overlay.title]; // <-- here
}
}
Of course, you need to manage all polygons (and others) with the generated IDs by yourself.
I have found a simple (single line) way to do that. Since GMSPolygon is a subclass of GMSOverlay we can cast him to be a GMSPolygon object, the delegate method will continue to work normally and it will treat it as a GMSPolygon object, changing it's .fillColor property.
Here's the code:
func mapView(mapView: GMSMapView, didTapOverlay overlay: GMSOverlay) {
/* Here we cast the GMSOverlay to be a GMSPolygon */
let overlay = overlay as! GMSPolygon
overlay.fillColor = UIColor.whiteColor()
/* Now the color of the polygon has changed on the map */
}
How to put the UITextView texting editing Magnifying Glass on the Google Maps GMSMapView
For example I have a GMSMapView to show my current location
I want to trigger a overlay Magnifying Glass view when calling delegate methods
mapView:didChangeCameraPosition: and mapView:willMove: in GMSMapViewDelegate
The purpose is to provide an overlay zooming subView according to the user tapping coordinates (like github.com/acoomans/iOS-MagnifyingGlass did on ImageView)
Please let me know if this is possible for Google Maps for iOS or if iOS MapKit can support this kind of customization
Update #2: mapView.addSubView(mapSubView) works now. But it pollutes the Main GMSMapView
Update #1: I tried mapView.addSubView, it seems does not work for GMSMapView although inherited from UIView
The intention of below code snippet is to retrieve user's touch point at the map and converts it into CGPoint for creating a second GMSMapView
func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
println("Tapping at (\(coordinate.latitude), \(coordinate.longitude))")
// 1. retrieve the user touch position as CLLocationCoordinate2D
let cameraPosition = GMSCameraPosition.cameraWithLatitude(coordinate.latitude, longitude: coordinate.longitude, zoom: 20)
// 2. convert it into CGPoint
let screenTouchPoints = mapView.projection.pointForCoordinate(coordinate)
// 3. set the CGRect for init the mapSubView
let frame = CGRectMake(screenTouchPoints.x, screenTouchPoints.y, 100, 100)
mapSubView = GMSMapView.mapWithFrame(frame, camera: cameraPosition)
// 4. Finally add to the main Map View
mapView.addSubview(mapSubView)
}
It seems plausible with a GMSMapView. Maybe have a second GMSMapView on top of the original(high corner radius to create a circle?) and animate the alpha and scale along with the zoom level within the second map. Do this whenever mapView:didChangeCameraPosition etc is called.
By taking reference to this Github Project, GMSMapView can also be magnified by putting the view hierarchy in this way:
View Controller > Magnifying View > The View want to be zoomed
The core rendering mechanism is
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, self.frame.size.width/2, self.frame.size.height/2 );
CGContextScaleCTM(context, scale, scale);
CGContextTranslateCTM(context, -touchPoint.x, -touchPoint.y + (self.scaleAtTouchPoint? 0 : self.bounds.size.height/2));
[self.viewToMagnify.layer renderInContext:context];
}
loupe = ACLoupe()
magnifyingGlass = ACMagnifyingGlass()
magnifyingView = ACMagnifyingView(frame: self.view.frame)
magnifyingView.magnifyingGlass = loupe
magnifyingView.addSubview(Your_Subview_here)
by this method the magnifying view can capture current frame context, because Magnifying View is the container of other views so that the capturing can show the current UI situation and zoom by 1.5 times (default scale factor)
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 :)