我有一个问题。 我现在的位置显示在地图视图然而在地图区域不会被放大到中心。 我试着服用罗布的建议采取跨度和区域出didUpdateToLocation方法,但我不能已经实现它的权利。 我不认为这是我认识呼吁setRegion在viewDidLoad中和我的按钮不被认可。 请在下面检查我的代码,并指出错误(或多个)。 我的目标是能够进出使用IBAction为按钮我的位置的放大。
。H
- (IBAction)zoomIn:(id)sender;
- (IBAction)zoomOut:(id)sender;
.M在viewDidLoad中
double miles = 0.5;
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/69.0;
MKCoordinateRegion region;
region.span = span;
[self.mapView setRegion:region animated:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
_mapView.mapType = MKMapTypeSatellite;
.M在我didUpdateToLocation方法。
[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
。放大:
- (IBAction)zoomIn:(id)sender
{
MKCoordinateSpan span;
span.latitudeDelta = _mapView.region.span.latitudeDelta * 2;
span.longitudeDelta = _mapView.region.span.latitudeDelta * 2;
MKCoordinateRegion region;
region.span = span;
region.center = _mapView.region.center;
[self.mapView setRegion:region animated:YES];
}
。缩小 :
- (IBAction)zoomOut:(id)sender
{
MKCoordinateSpan span;
span.latitudeDelta = _mapView.region.span.latitudeDelta / 2;
span.longitudeDelta = _mapView.region.span.latitudeDelta / 2;
MKCoordinateRegion region;
region.span = span;
region.center = _mapView.region.center;
[self.mapView setRegion:region animated:YES];
}
你可以得到当前region
,乘或除的span
由两个酌情(将在变焦,变焦上进行相乘),然后设置region
:
- (IBAction)zoomIn:(id)sender {
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta /= 2.0;
region.span.longitudeDelta /= 2.0;
[self.mapView setRegion:region animated:YES];
}
- (IBAction)zoomOut:(id)sender {
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta = MIN(region.span.latitudeDelta * 2.0, 180.0);
region.span.longitudeDelta = MIN(region.span.longitudeDelta * 2.0, 180.0);
[self.mapView setRegion:region animated:YES];
}
如果你想拥有的地图自动缩放到你的位置,你可以使用:
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
如果你想手动做到这一点,你可以不喜欢以下。 首先,定义您是否已经或没有放大的类属性:
@property (nonatomic) BOOL alreadySetZoomScale;
然后改变你的didUpdateLocations
(或didUpdateToLocation
)检查此并设置变焦倍数一次:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* newLocation = [locations lastObject]; // if less than zero, then valid lat and long not found
if (newLocation.horizontalAccuracy < 0)
return;
if (!self.alreadySetZoomScale)
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1609, 1609); // 1 mile = 1609.34 meters
self.mapView.region = region;
[self.mapView setRegion:region animated:YES];
self.alreadySetZoomScale = YES;
}
else
{
[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// if prior to iOS 6, use this old `MKMapViewDelegate` method, but call our
// other routine.
if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
[self locationManager:manager didUpdateLocations:@[newLocation]];
}
这基本上说,“如果我没有在放大的是,设置基于区域newLocation
以及一定数量的周围的位置公尺,但是我已经做了,刚刚设置的中心坐标根据我目前的位置,但不改变变焦倍数(如果我已经放大或缩小),这也做一些有条件的iOS版本号逻辑(使用这里显示宏 ),确保如果最终用户到6.0运行iOS之前,它会调用我们的更新方法。
顺便说一句,如果你正在展示的地图(如用户位置self.mapView.showsUserLocation = YES;
你可能希望有这种didUpdateLocations
也删除与该关联覆盖MKUserLocation
移动地图中心之前,否则可以保留旧覆盖围坐:
[self.mapView removeOverlays:self.mapView.overlays];
试试这个。 但我尚未对其进行测试。
- (IBAction)zoomIn:(id)sender {
MKCoordinateSpan span;
span.latitudeDelta = _mapView.region.span.latitudeDelta * 2;
span.longitudeDelta = _mapView.region.span.latitudeDelta * 2;
MKCoordinateRegion region;
region.span = span;
region.center = _mapView.region.center;
[self.mapView setRegion:region animated:YES];
}
- (IBAction)zoomOut:(id)sender {
MKCoordinateSpan span;
span.latitudeDelta = _mapView.region.span.latitudeDelta / 2;
span.longitudeDelta = _mapView.region.span.latitudeDelta / 2;
MKCoordinateRegion region;
region.span = span;
region.center = _mapView.region.center;
[self.mapView setRegion:region animated:YES];
}
OK,这是我用得到我的2个IBAction为按钮(zoomIn和缩小(ZoomOut))与我的MapView工作结束。 当您打开应用程序时,会显示用户的位置和地图放大并且围绕它的中心。 然后,您可以用按钮来放大或2倍缩小*大感谢Rob谁发现我的方式。
。H
- (IBAction)zoomIn:(id)sender;
- (IBAction)zoomOut:(id)sender;
.M
@interface LocationViewController ()
@property (nonatomic) BOOL alreadySetZoomScale;
@end
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if (!_alreadySetZoomScale)
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1609, 1609); // 1 mile = 1609.34 meters
self.mapView.region = region;
[self.mapView setRegion:region animated:YES];
_alreadySetZoomScale = YES;
}
else
{
[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
}
- (IBAction)zoomIn:(id)sender {
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta /= 2.0;
region.span.longitudeDelta /= 2.0;
self.mapView.region = region;
}
- (IBAction)zoomOut:(id)sender {;
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta *= 2.0;
region.span.longitudeDelta *= 2.0;
self.mapView.region = region;
}
我还有其他几个来电的MKMapView像self.mapView.showsUserLocation = YES; 在viewDidLoad中,但是这几乎是它。
雨燕2.0:
使用扩展:
extension MKMapView{
func zoomInPinAnnotationLocation(targetMapViewName : MKMapView?, delta: Double)
{
var region: MKCoordinateRegion = targetMapViewName!.region
region.span.latitudeDelta /= delta
region.span.longitudeDelta /= delta
targetMapViewName!.region = region
}
func zoomOutPinAnnotationLocation(targetMapViewName : MKMapView?,delta: Double)
{
var region: MKCoordinateRegion = targetMapViewName!.region
region.span.latitudeDelta *= delta
region.span.longitudeDelta *= delta
targetMapViewName!.region = region
}
}
用法:
var mapViewZoomStepperValue: Double = -1.0
@IBOutlet weak var mapViewZoomStepper: UIStepper!
@IBAction func mapViewZoomStepperValueChanged(sender: AnyObject) {
if (mapViewZoomStepper.value > mapViewZoomStepperValue)
{
mapViewZoomStepperValue = mapViewZoomStepperValue + 1.0
//Zoom In
detailMapView.zoomInPinAnnotationLocation(detailMapView, delta: 3.0)
}
else
{
mapViewZoomStepperValue = mapViewZoomStepper.value - 1.0
//Zoom Out
detailMapView.zoomOutPinAnnotationLocation(detailMapView, delta: 3.0)
}
}
斯威夫特3:
import GoogleMaps
import GooglePlaces
import CoreLocation
import UIKit
class LocationMapView: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
var mapViewZoomStepperValue: Float = 0.0
@IBOutlet weak var mapViewZoomStepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
mapViewZoomStepper.minimumValue = -100
}
@IBAction func mapViewZoomStepperValueChanged(_ sender: Any) {
if (Float(mapViewZoomStepper.value) > mapViewZoomStepperValue){
mapViewZoomStepperValue = Float(mapViewZoomStepper.value)
zoomIn()
}else{
mapViewZoomStepperValue = Float(mapViewZoomStepper.value)
zoomOut()
}
}
func zoomIn() {
print("Zoom in!!")
if mapView.camera.zoom == mapView.maxZoom {
return
}
let currentZoom = mapView.camera.zoom + 1
mapViewZoomController(currentZoom)
}
func zoomOut() {
print("Zoom out!!")
if mapView.camera.zoom == mapView.minZoom {
return
}
let currentZoom = mapView.camera.zoom - 1
mapViewZoomController(currentZoom)
}
func mapViewZoomController(_ level: Float) {
let point: CGPoint = mapView.center
let coor: CLLocationCoordinate2D = mapView.projection.coordinate(for: point)
let camera = GMSCameraPosition.camera(withLatitude: coor.latitude, longitude: coor.longitude, zoom: Float(level))
mapView.animate(to: camera)
}
}
你可以达到3行代码最基本的
var region: MKCoordinateRegion = self.mapView.region
region = MKCoordinateRegionMake(self.mapView.centerCoordinate, MKCoordinateSpanMake(0.005, 0.005));
self.mapView.setRegion(region, animated: true)