I dropped the maps from iOS and switched to google maps. Now it shows my position but it doesn't load the map. It is gray / yellow with in the bottom left Google.
This is my implementation:
#import <CoreLocation/CoreLocation.h>
#interface GoogleMapViewController ()
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
#implementation GoogleMapViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
_mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
_mapView.myLocationEnabled = YES;
self.view = _mapView;
[self initLocationManager];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = _mapView;
}
- (void) initLocationManager
{
self.locationManager = [[CLLocationManager alloc] init] ;
self.locationManager.delegate = (id)self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
[self.locationManager setDistanceFilter:10.0f] ;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"New locations");
[self.locationManager stopUpdatingLocation];
_mapView.camera = [GMSCameraPosition cameraWithTarget:newLocation.coordinate
zoom:6];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
This is basically the tutorial demo app from Google Dev page, and I'm not sure why it's not loading the map.
This is what I get:
I found the problem. You need to set the initial camera zoom level on 1 and then zoom in on your current location. That way you can still zoom out and have the whole map available.
If you take a zoomlevel 14 as initial, google maps doesn't load the rest of the map I think.
Related
I'm using Google Maps SDK for iOS (version 2.1.1) with Xcode 8. I added a UIView and set its class to GMSMapView and linked up with an IBOutlet called mapView.
double bearing;
int zoomLevel;
- (void)viewDidLoad {
[super viewDidLoad];
bearing = 30;
zoomLevel = 20;
_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
if([CLLocationManager headingAvailable]) {
_locationManager.headingFilter = 5;
[_locationManager startUpdatingHeading];
}
mapView.myLocationEnabled = YES;
mapView.settings.tiltGestures = NO;
mapView.settings.scrollGestures = NO;
// obtain my position
CLLocation *myLocation = mapView.myLocation;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
longitude:myLocation.coordinate.longitude
zoom:zoomLevel
bearing:bearing
viewingAngle:45];
mapView.camera = camera;
}
The View Controller uses 2 delegates : CLLocationManagerDelegate and GMSMapViewDelegate.
Updating map location is not a problem, but when I rotate the map, since the bearing value has not changed at all, once the location is updated, the bearing is reset and cancelled the rotation. How can I update the bearing after I rotate the map?
Here is the didUpdateLocations: delegate function:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *myLocation = [locations lastObject];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
longitude:myLocation.coordinate.longitude
zoom:zoomLevel
bearing:bearing
viewingAngle:45];
[mapView animateToCameraPosition:camera];
}
My idea is to rotate the map exactly like the mobile game PokemonGO.
Following Online tutorials I made a MKMapView working, but I have a couple of problems:
First problem: Map show my current position, but if I try to scroll that map It will automatically return to my position, so, for example, I am not able to see what streets are nearby, because map will center back to my current position.
Second problem: I'd like to implement some "pinch to zoom" as if you tap on my map it will get that tap point's longitude and latitude.
I surely messed up my code, because I mixed two tutorial in one ... trying to archive what I needed from my "app". :-)
This is in my .h:
#interface mappa2 : UIViewController <CLLocationManagerDelegate> {
MKMapView *mapview;
CLLocationManager *manager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapview;
And this is my messy .m
#synthesize mapview;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Procedura per TAP:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.mapview addGestureRecognizer:tapRecognizer];
[self startMap]; }
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"Errore %#",error);
NSLog(#"Non ho potuto calcolare la posizione");
}
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"Location: %#",newLocation);
CLLocation *currentLocation = newLocation;
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] >0 ) {
placemark = [placemarks lastObject];
}
else NSLog(#"%#",error.debugDescription);
}];
MKCoordinateRegion region = { {0.0, 0.0}, {0.0, 0.0} };
region.center.latitude = currentLocation.coordinate.latitude;
region.center.longitude = currentLocation.coordinate.longitude;
region.span.longitudeDelta = 0.001f;
region.span.latitudeDelta = 0.001f;
[mapview setRegion: region animated:YES];
[mapview setScrollEnabled:YES];
}
- (void) startMap {
manager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation];
mapview.mapType = MKMapTypeStandard;
mapview.showsUserLocation = YES;
}
So .. I'd like that my map could point to my current position when "view did load", but then I'd be able to move around that point! ... and zooming in and out with gesture... is that too much for a newbie?
Thank in advance!
For the first issue, you're probably updating and resetting your location via MKMapView delegate.
Mk Mapview keeps resetting on users location?
For the second issue, the question probably contains the answer you are looking for. If not, the answers have it, plus extra code, for sure.
Get the coordinates of a point from mkmapview on iphone
For some reason, I can't seem to get tap and longtap and all the other delegate events to work in my simulator. I think I installed everything correctly because the map shows up on the screen, with a custom button click I can put a marker on the map, but it will not recognize the delegate events. Am I doing something wrong???
mapviewcontroller.h
#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>
#interface MapViewController : UIViewController <GMSMapViewDelegate, CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet UIView *mapContainer;
- (IBAction)addmarker:(id)sender;
#end
mapviewcontroller.m
#import "MapViewController.h"
#interface MapViewController ()
#end
#implementation MapViewController{
GMSMapView *mapView;
}
#synthesize mapContainer;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
mapView.settings.compassButton = YES;
//CLLocation *myLoc = mapView.myLocation;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.mapContainer.frame.size.width, self.mapContainer.frame.size.height) camera:camera];
[self.mapContainer addSubview:mapView];
}
- (void) mapView: (GMSMapView *) mapView didTapAtCoordinate: (CLLocationCoordinate2D) coordinate{
NSLog(#"%f, %f", coordinate.latitude, coordinate.longitude);
}
- (void) mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate{
NSLog(#"tapped");
}
- (void) mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker{
NSLog(#"tapped info");
}
- (void) mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker{
NSLog(#"dragged");
}
- (IBAction)addmarker:(id)sender {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(-33.86, 151.20);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = #"hello";
marker.map = mapView;
}
#end
The only thing that works is the IBAction. Also my location and compass buttons do not show up. Any ideas of why it's not working? I declared the delegates...
You need to set all properties after the object initialization.
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.mapContainer.frame.size.width, self.mapContainer.frame.size.height) camera:camera];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
mapView.settings.compassButton = YES;
[self.mapContainer addSubview:mapView];
}
I am using the Google Maps iOS API. None of the delegate methods are firing. How do I make them work?
MapViewController.h:
#interface MapViewController : UIViewController <GMSMapViewDelegate>
#property (strong, nonatomic) IBOutlet GMSMapView *mapView_;
#end
#import "MapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#interface MapViewController ()
#end
MapViewController.m
#implementation MapViewController
#synthesize mapView_;
GMSCircle *circ;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)loadView {
mapView_.delegate = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10
longitude:10
zoom:15];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
mapView_.mapType = kGMSTypeHybrid;
CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(10, 10);
GMSCircle *circ = [GMSCircle circleWithPosition:circleCenter
radius:10];
circ.tappable = true;
[circ setFillColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:.5]];
circ.map = self.mapView_;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
{
NSLog(#"in didTapOverlay");
[overlay isKindOfClass:[circ class]];
if ([overlay isKindOfClass:[circ class]]) {
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(10, 10);
marker.title = #"Place";
marker.snippet = #"Sub Place";
marker.map = mapView_;
}
}
- (void) mapView:(GMSMapView *) mapView willMove:(BOOL) gesture
{
NSLog(#"In willMove");
}
#end
I think you are setting the delegate property to early
try this :
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10
longitude:10
zoom:15];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
self.view = mapView_;
Set the delegate in the ViewDidLoad
I am currently working with Google Maps API in my project. I am trying to set the default camera/zoom to the users location. I do this:
#implementation ViewController{
GMSMapView *mapView_;
}
#synthesize currentLatitude,currentLongitude;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView_.settings.myLocationButton = YES;
mapView_.myLocationEnabled = YES;
}
- (void)loadView{
CLLocation *myLocation = mapView_.myLocation;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = #"Current Location";
marker.map = mapView_;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
longitude:myLocation.coordinate.longitude
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView_;
NSLog(#"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}
However, it does not work, since when I do
NSLog(#"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
it returns 0, 0, and it does not give the current location coordinates. How can I properly get the user's coordinates?
.h
#import <CoreLocation/CoreLocation.h>
#property(nonatomic,retain) CLLocationManager *locationManager;
.m
- (NSString *)deviceLocation
{
NSString *theLocation = [NSString stringWithFormat:#"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
return theLocation;
}
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
answered here.
When an app first starts it may not yet know your location, as it usually takes a while for the GPS device to lock on to your location (if it has just been started), and especially if this is the first time the application has been run, and so the user hasn't yet answered the prompt to give the app access to their location. Also it seems like mapView.myLocation is always empty (either nil or has coordinates 0,0) when the map view has just been created.
So you will need to wait until the user's location is known, and then update the camera position.
One way might be using the code at how to get current location in google map sdk in iphone as suggested by Puneet, but note that the sample code there is missing the details of setting up the location manager (like setting the location manager's delegate), which might be why it didn't work for you.
Another option could be to use KVO on mapView.myLocation, as described here: about positioning myself,some problems
By the way in your sample code you are accessing mapView.myLocation before you create the mapView, and so the location would always be nil anyway.
I just Downloaded the new GoogleMap SDK Demo for iOS. Here is what I have seen from the source code that how the "Current Location" is achieved via KVO.
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
#import "SDKDemos/Samples/MyLocationViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#implementation MyLocationViewController {
GMSMapView *mapView_;
BOOL firstLocationUpdate_;
}
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
// Listen to the myLocation property of GMSMapView.
[mapView_ addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.view = mapView_;
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});
}
- (void)dealloc {
[mapView_ removeObserver:self
forKeyPath:#"myLocation"
context:NULL];
}
#pragma mark - KVO updates
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!firstLocationUpdate_) {
// If the first location update has not yet been recieved, then jump to that
// location.
firstLocationUpdate_ = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}
#end
Hope it can help you.
Just in case anyone wants DrDev's excellent answer in Swift 3:
var locationManager: CLLocationManager?
func deviceLocation() -> String {
let theLocation: String = "latitude: \(locationManager.location.coordinate.latitude) longitude: \(locationManager.location.coordinate.longitude)"
return theLocation
}
func viewDidLoad() {
locationManager = CLLocationManager()
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
// 100 m
locationManager.startUpdatingLocation()
}