繁体   English   中英

iOS上的Google Maps SDK(非模拟器!),myLocation为0.000000

[英]Google Maps SDK on iOS (Not Simulator!), myLocation is 0.000000

编辑:这不一定是参考模拟器,使用Debug-> Location的东西。 它显示0.000000我是在模拟器上还是在真实手机上。

我正在使用谷歌地图试图制作一个可以根据我当前位置做事情的应用程序。

当我打开应用程序时,地图上的蓝点位置在我的实际(正确)位置,但我放在“myLocation”的标记位于0.000000,0.000000(这也是打印到控制台的值)

当我打开应用程序和/或做一些事情来获取蓝点所在的实时坐标时,我怎样才能使我的标记位置在当前位置? (这样我随时可以访问实时纬度和经度)

提前致谢!

这是我的代码:

#import "MyViewController.h"
#import <GoogleMaps/GoogleMaps.h>


@interface MyViewController ()
@property (nonatomic, readwrite) NSString *coord;
@end

@implementation MyViewController

GMSMapView *mapView_;


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view layoutIfNeeded];

    // Do any additional setup after loading the view, typically from a nib.
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    CLLocation *myLocation = mapView_.myLocation;
    GMSCameraPosition *camera = [GMSCameraPosition     cameraWithLatitude:myLocation.coordinate.latitude
                                                            longitude:myLocation.coordinate.longitude
                                                             zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;


self.view = mapView_;




// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
//    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude,     myLocation.coordinate.longitude);
marker.title = @"Current:";
float lat = mapView_.myLocation.coordinate.latitude;
float lon = mapView_.myLocation.coordinate.longitude;
_coord = [NSString stringWithFormat:@"%f %f",lat,lon];
printf("%f %f",lat,lon);
marker.snippet = (_coord);
marker.map = mapView_;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@interface ViewController () <GMSMapViewDelegate>
{
 GMSMapView *mapView_;
}
@end

@implementation ViewController

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
   [mapView_ addObserver:self forKeyPath:@"myLocation" options:0 context:nil];
}

- (void)dealloc
{
    [mapView_ removeObserver:self forKeyPath:@"myLocation"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"myLocation"])
    {
       CLLocation *location = [object myLocation];

        NSLog(@"Location, %@,", location);

        CLLocationCoordinate2D target =
        CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);

        [mapView_ animateToLocation:target];
        [mapView_ animateToZoom:17];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    GMSCameraPosition *cam = [GMSCameraPosition cameraWithLatitude:mapView_.myLocation.coordinate.latitude longitude:mapView_.myLocation.coordinate.longitude zoom:15];
    mapView_ = [GMSMapView mapWithFrame:CGRectMake(20, 20, self.view.frame.size.width, self.view.frame.size.height - 64) camera:cam];

   self.view = mapView_;

    mapView_.delegate = self;

    mapView_.userInteractionEnabled = YES;
    mapView_.myLocationEnabled = YES;

    mapView_.mapType = kGMSTypeNormal;
    mapView_.indoorEnabled = YES;
    mapView_.accessibilityElementsHidden = NO;

    mapView_.settings.scrollGestures = YES;
    mapView_.settings.zoomGestures = YES;
    mapView_.settings.tiltGestures = YES;
    mapView_.settings.rotateGestures = YES;

    mapView_.settings.compassButton = YES;
    mapView_.settings.myLocationButton = YES;
    mapView_.settings.indoorPicker = YES;

}

为此,您需要CLLocationManger类的帮助

IN .h文件

CLLocationManager *currentLocationManager;
CLLocation *myLocation ;

IN .m文件

#import <CoreLocation/CoreLocation.h>


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view layoutIfNeeded];

currentLocationManager = [[CLLocationManager alloc] init];
    currentLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
    currentLocationManager.distanceFilter = DISTANCE_FILTER_VALUE_STATIONARY;
    [self.currentLocationManager setDelegate:self];
    [currentLocationManager startUpdatingLocation];

}

#pragma mark
#pragma mark for Location manager Delegate Methods

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {

myLocation = [locations lastObject];

// Now create maker on current location
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = myLocation.coordinate;
marker.title = @"Current:";
float lat = mapView_.myLocation.coordinate.latitude;
float lon = mapView_.myLocation.coordinate.longitude;
_coord = [NSString stringWithFormat:@"%f %f",lat,lon];
printf("%f %f",lat,lon);
marker.snippet = (_coord);
marker.map = mapView_;

}

这会在当前位置创建标记。 您将根据位置管理器上设置的“距离过滤器”和“accurracy”获取位置更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM