我有一个问题。我的当前位置以地图视图显示并居中,但地图区域不会放大。我尝试通过从didUpdateToLocation方法中取出span和region来获取Rob的建议,但我必须没有正确实现它。我不认为它在viewDidLoad中识别我对setRegion的调用,并且我的按钮无法被识别。请检查下面的代码,并指出错误。我的目标是能够使用IBAction按钮放大和缩小我的位置。
·H
- (IBAction)zoomIn:(id)sender;
- (IBAction)zoomOut:(id)sender;
viewDidLoad中的.m
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;
[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
.Zoom In:
- (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];
}
答案 0 :(得分:10)
您可以根据需要获取当前region
,将span
乘以或除以2(除以放大,乘以缩小),然后设置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版本号逻辑(使用macros shown here),确保是否最终用户在6.0之前运行iOS,它将调用我们更新的方法。
顺便说一句,如果您在地图上显示用户位置(例如self.mapView.showsUserLocation = YES;
),您可能希望此didUpdateLocations
也删除与MKUserLocation
相关联的叠加层在移动地图中心之前,否则它可以留下旧的叠加层:
[self.mapView removeOverlays:self.mapView.overlays];
答案 1 :(得分:1)
试试这个。但我还没有测试过。
- (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];
}
答案 2 :(得分:1)
好的,这就是我用来获取我的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中,但就是这样。
答案 3 :(得分:0)
Swift 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)
}
}
答案 4 :(得分: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)
}
}
答案 5 :(得分:-1)
你可以在3行代码中实现最基本的代码
var region: MKCoordinateRegion = self.mapView.region
region = MKCoordinateRegionMake(self.mapView.centerCoordinate, MKCoordinateSpanMake(0.005, 0.005));
self.mapView.setRegion(region, animated: true)