美文网首页
GoogleMap小结

GoogleMap小结

作者: warm_iOS | 来源:发表于2019-03-20 11:38 被阅读0次

获取当前定位

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_isChina != YES)
    {
        [_locationManager startUpdatingLocation];
    }
}
- (void)startLocationManager
{
    if (_locationManager == nil)
    {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
        _locationManager.distanceFilter = 500;
    }
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    NSLog(@"获取当前定位成功");
    CLLocation *curLocation = [locations lastObject];
    // 通过location  或得到当前位置的经纬度
    CLLocationCoordinate2D curCoordinate2D = curLocation.coordinate;
    _googleMapCamera = [GMSCameraPosition cameraWithLatitude:curCoordinate2D.latitude longitude:curCoordinate2D.longitude zoom:15];
    _currentLocation = CLLocationCoordinate2DMake(curLocation.coordinate.latitude, curLocation.coordinate.longitude);
    self.googleMapView.camera = _googleMapCamera;//这句话很重要很重要,将我们获取到的经纬度转成影像并赋值给地图的camera属性
    [self.locationManager stopUpdatingLocation];//定位成功后停止定位
}

创建map

- (GMSMapView *)googleMapView
{
    if (_googleMapView == nil) {
        _googleMapCamera = [GMSCameraPosition cameraWithLatitude:[TJLocationManager shareInstance].locationModel.baiduGPS.latitude
                                                                longitude:[TJLocationManager shareInstance].locationModel.baiduGPS.longitude
                                                                     zoom:15];
        _googleMapView = [GMSMapView mapWithFrame:CGRectMake(0, NAV_HEIGHT,kScreenWidth , kScreenHeight-NAV_HEIGHT - 40) camera:_googleMapCamera];
        _googleMapView.myLocationEnabled = YES;
        _googleMapView.delegate = self;
    }
    return _googleMapView;
}

添加标记

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(model.latitude.doubleValue, model.longitude.doubleValue);
            GMSMarker *marker = [GMSMarker markerWithPosition:position];
            marker.title = model.username;
            marker.icon = [UIImage imageNamed:@"ditushangjiabiaoz"];
            marker.map = _googleMapView;
            [weakSelf.annotauions addObject:marker];

设置默认选中标记

GMSMarker *marker = weakSelf.annotauions[didSelectedIndex];
                CLLocationCoordinate2D position = marker.position;
                weakSelf.googleMapCamera = [GMSCameraPosition cameraWithLatitude:position.latitude
                                                              longitude:position.longitude
                                                                   zoom:15];
                [weakSelf.googleMapView setCamera:weakSelf.googleMapCamera];
                weakSelf.googleMapView.selectedMarker = marker;

清空所有标记

[self.googleMapView clear];

相关文章

网友评论

      本文标题:GoogleMap小结

      本文链接:https://www.haomeiwen.com/subject/ylkzmqtx.html