GoogleMap小结
2019-03-20 本文已影响0人
warm_iOS
获取当前定位
- (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];