I am using iOS Google Maps in my app. I've been looking for memory leaks as of lately as I noticed the Memory started spiking whenever I would zoom out. Both on simulator and my actual iPhone X device experience such memory spikes when zooming out. I am also zooming out because I am using the marker clustering feature.
I've done some research on different ways to detect memory leaks such as enabling sound actions on breakpoints and looking at Backtrace tree for strong references. It seems that there is a strong reference when I configure my GMSMapView (Code below).
func configureMapView() {
guard let location = locationManager.location else { return }
let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 16.5)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isMyLocationEnabled = true
mapView.settings.compassButton = true
mapView.settings.rotateGestures = false
mapView.settings.tiltGestures = false
view = mapView
}
Xcode is pointing to this line that has a strong reference.
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
Is this strong reference the reason why memory usage spikes up when zooming out? Also, not entirely sure how to fix this strong reference with Google Maps, any help would be appreciated!
Related
I want to display Gunshot information on the Maps app. A gunshot has 2 main properties:
location - point from which the shot was fired
direction/angle - relative to Compass North
Using the following code snippet, I can open the Maps app with a specific place-mark.
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(gps_lat.doubleValue, gps_long.doubleValue);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:self.pUnit.unitLabel];
[mapItem openInMapsWithLaunchOptions:nil];
Is it possible to add angle/direction information along with the place mark? Basically the place-mark should rotate along with the map so as to clearly depict the direction of the gunshot.
you can achieve rotation of marker using google maps and rotation property of marker.
though you want obj-c code, my swift code will be helpful to understand you what I meant.
replace MARKER_IMAGE_NAME with your image
1) setup google map
let camera = GMSCameraPosition.camera(withLatitude: firstPoint.latitude,
longitude: firstPoint.longitude, zoom: 14)
let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + 20
let frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - navHeight)
let GMapView = GMSMapView.map(withFrame: frame, camera: camera)
self.view.addSubview(GMapView)
2) prepare marker
let bearing:Double = 90
var marker = GMSMarker()
marker.icon = UIImage(named: MARKER_IMAGE_NAME)
marker.appearAnimation = GMSMarkerAnimation.pop
3) apply rotation
marker.rotation = bearing
4) add marker to google map
marker.map = GMapView
I am using Google Map service GMSMapview in my iOS Application. In that I have two CLLocationCoordinates. One is Current Location and other is Destination Location. I am trying to fit the map within those two places.
But the camera position is not set within those two points. I tried with the following code,
CLLocationCoordinate2D start = CLLocationCoordinate2DMake(newLoc.coordinate.latitude, newLoc.coordinate.longitude);
CLLocationCoordinate2D end = CLLocationCoordinate2DMake(_retailerVO.R_Latitude, _retailerVO.R_Longitude);
GMSCoordinateBounds *gBounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:start coordinate:end];
GMSCameraPosition *gCamera = [mapView_ cameraForBounds:gBounds insets:UIEdgeInsetsZero];
mapView_.camera = gCamera;
Is there any way to achieve what I am looking for? All suggestions are appreciated.
In Swift
let bounds = GMSCoordinateBounds(coordinate: sourcePosition, coordinate: endPosition)
let camera: GMSCameraUpdate = GMSCameraUpdate.fit(bounds)
// let cameraWithPadding: GMSCameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 100.0) (will put inset the bounding box from the view's edge)
self.mapView.animate(with: camera)
Add padding with bounds, it works for me, hope it helps you,
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:start coordinate:end];
[self.viewMapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:100.0f]];
Why is my google map view showing questionmarks instead of letters:
My code for implementing the map view:
let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
v1.8.0 (May 2014) has an update regarding an issue raised similar to you. The ticket raised suggests it may have something to do with the language settings. So, you can check out if the language setting was changed and hopefully it solves the issue. Also check if you're using the latest version of the Maps iOS SDK.
I'm trying to set a custom starting region in an iOS app using MKMapView's setRegion. The app runs fine, and a map appears, but no matter what I try I can't get the region to change. I've tried many tutorials and solutions, but none are working. Here's my code:
-(void)viewDidAppear:(BOOL)animated{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.001;
span.longitudeDelta = 0.001;
region.span = span;
region.center.latitude = 100;
region.center.longitude = 100;
[mapView setRegion:(region) animated:(TRUE)];
}
I have the MKMapView and Core Location frameworks added to the projects properly, I import MapKit.h, and I declare mapView, so I don't know why it's not working.
Thank you,
Jacob
It sounds like the map view's IBOutlet is not connected to the map view control in the xib/storyboard.
If it's not connected, the mapView variable will be nil and calling methods on it will do nothing.
In the xib/storyboard, right-click on View Controller and connect the mapView outlet to the map view control.
Additionally, although not necessary for just setting the region, also connect the map view's delegate outlet to the View Controller. This will be required if you later implement any map view delegate methods. If this is not done, the delegate methods won't get called.
Another separate point:
In your first code example, you are setting the region's center to 100, 100. Please note that this is an invalid coordinate. If the map view was actually connected, setting the center to this would have caused a crash with "Invalid Region". Latitude must be from -90 to +90 (longitude must be from -180 to +180).
By the way, the new coordinate you're trying in the code posted in the comment (26, 80) is in India. Since you're setting the span to a relatively small value, you'll need to zoom out a lot to see this.
However it was set as answered, I will let here an example of what worked for me, when I had no clue why setRegion was not working at all. In my case the problem was the I've initiated the MKMapView without a frame, e.g.:
override func viewDidLoad() {
...
self.mapView = MKMapView() // Wrong!
...
self.mapView.setRegion(...) // Does not work!
}
It looks like that the whole region thing is calculated with regards the the initial (CGRect). It worked for me by doing:
override func viewDidLoad() {
...
self.mapView = MKMapView(frame: CGRect(0, 108, self.mapView.bounds.width, self.mapView.bounds.height))
self.mapView.setRegion(...) // Oh, it works!
}
This problem was described here too.
I am building an iOS app using storyboards and Google Maps. Using iOS6
My application features the split view navigation as seen in the facebook app
On my left view I am selecting an item in a list which has lat/long cords and showing it on my map on the following method
- (void)viewWillAppear:(BOOL)animated
I would like to remove all markers in this method before I add another one (so only one marker is on the map), is there a way to do this? Below is my code to add a marker to the mapView
Thanks in advance - Jon
- (void)loadView
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:poi.lat
longitude:poi.lon
zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.myLocationEnabled = YES;
self.view = mapView;
mapView.mapType = kGMSTypeHybrid;
//Allows you to tap a marker and have camera pan to it
mapView.delegate = self;
}
-(void)viewWillAppear:(BOOL)animated
{
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = CLLocationCoordinate2DMake(poi.lat, poi.lon);
options.title = poi.title;
options.snippet = poi.description;
options.icon = [UIImage imageNamed:#"flag-red.png"];
[mapView addMarkerWithOptions:options];
[mapView animateToLocation:options.position];
[mapView animateToBearing:0];
[mapView animateToViewingAngle:0];
}
To remove all markers
mapView.clear()
To remove a specific marker
myMarker.map = nil
To remove all markers simple do:
[self.mapView clear];
Please refer to the Google Map documentation: Google Maps SDK for iOS
Please refer to the section title "Remove a marker". Always check documentation for such methods.
mapView.clear()
// It will clear all markers from GMSMapView.
To remove a single marker from map.
yourMarkerName.map = nil
To remove all markers from map.
yourMapViewName.clear()
It's a little bit tricky here if you want to remove only one marker among a group of your markers.
let marker = GMSMarker(position: coordinate) //
marker.icon = UIImage(named: "ic_pin_marker")
marker.map = mapView
mapView.selectedMarker = marker // the specific marker you want to remove or modify by set it as selectedMarker on the map.
then you want to remove or modify
mapView.selectedMarker.map = nil //example to remove the marker form the map.
Hopefully useful with your condition.
mapView.clear() is not a good idea .
because The Places SDK for iOS enforces a default limit of 1,000 requests per 24 hour period.(If your app exceeds the limit, the app will start failing. Verify your identity to get 150,000 requests per 24 hour period.)
whit mapView.clear() the requests increase .
the best way is clear each marker and polylines .