Ios 地图:can';t放大当前位置

Ios 地图:can';t放大当前位置,ios,objective-c,location,mkmapview,cllocationmanager,Ios,Objective C,Location,Mkmapview,Cllocationmanager,问题:首次加载地图时,地图不会放大用户的当前位置 我在努力理解我做错了什么。正如你所知道的,我对这一点还很陌生,所以请友善一点:) h #导入 #进口 #进口 @接口MapViewController:UIViewController @属性(保留,非原子)IBOutlet MKMapView*shopMapView; @财产(保留,非原子)CLLocationManager*locationManager; @结束 m #导入“MapViewController.h” #导入“ShopMod

问题:首次加载地图时,地图不会放大用户的当前位置

我在努力理解我做错了什么。正如你所知道的,我对这一点还很陌生,所以请友善一点:)

h

#导入
#进口
#进口
@接口MapViewController:UIViewController
@属性(保留,非原子)IBOutlet MKMapView*shopMapView;
@财产(保留,非原子)CLLocationManager*locationManager;
@结束
m

#导入“MapViewController.h”
#导入“ShopModel.h”
#导入“ShopAnnotation.h”
#导入“ShopAnnotationView.h”
#导入“CoordinatingController.h”
@接口MapViewController()
@结束
@MapViewController的实现
@综合商店地图视图;
@综合定位经理;
#pragma-mark-Helper方法-
-(无效)重置所有说明
{
//用于重置地图上所有注释的代码
}
-(无效)查找位置
{
NSString*city=[[ShopModel sharedInstance]用户选择的城市];
如果(城市==零)
返回;
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
n错误*错误=nil;
NSString*urlString=[NSString stringWithFormat:@]http://maps.google.com/maps/geo?q=%@&输出=csv“,
[city stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]编码:NSUTF8STRING编码错误:&错误];
NSArray*listItems=[locationString组件由字符串分隔:@“,”];
双纬度;
双经度;
如果([listItems count]>=4&&[listItems objectAtIndex:0]IsequalString:@“200”]){
纬度=[[listItems对象索引:2]双值];
经度=[[listItems对象索引:3]doubleValue];
}
否则{
//错误
}
CLLocation*location=[[CLLocation alloc]initWithLatitude:纬度经度:经度];
[self-performSelectorOnMainThread:@selector(zoomInLocation:)withObject:location waitUntilDone:NO];
[位置释放];
[池释放];
}
-(void)zoomInLocation:(CLLocation*)位置
{
shopmappview.region=mkcoordinaeregionmakewithdistance(location.coordination,800800);
}
#pragma标记-MapViewDelegate-
-(MKAnnotationView*)地图视图:(MKMapView*)商店地图视图注释:(id)注释
{
ShopAnnotationView*视图=(ShopAnnotationView*)[shopMapView出列可重用AnnotationViewWithIdentifier:@“测试”];
如果(视图==NULL)
{
视图=[[ShopAnnotationView alloc]initWithAnnotation:注释重用标识符:@“测试”]自动释放];
[查看设置CanShowCallout:是];
view.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypedTailDisclosure];
}
返回视图;
}
-(无效)地图视图:(MKMapView*)商店地图视图注释视图:(MKAnnotationView*)视图调用访问控制点击:(UIControl*)控制
{
[[CoordinationController sharedInstance]requestViewChangeByObject:view];
}
-(无效)变更城市
{
[self-performSelectorInBackground:@selector(findLocation)with object:nil];
}
#pragma标记-ViewController生命周期-
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
self.navigationItem.title=NSLocalizedString(@“Map”,@“Map”);
[self-performSelectorInBackground:@selector(findLocation)with object:nil];
[自我重置所有说明];
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(resetAllAnnotations)名称:@“AllAnnotationSupdate”对象:nil];
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(changeCity)name:@“CityChanged”对象:nil];
}
-(无效)视图卸载
{
[自设置映射视图:无];
[超级视频下载];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
-(无效)解除锁定{
[[NSNotificationCenter defaultCenter]移除观察者:self];
[商店地图视图发布];
[super dealoc];
}
@结束

我不知道它是否是StackOverflow中的一种类型,但在您的代码中,我看到zoom方法以大写开头,而您用小写来调用它。 我建议如下重命名缩放方法:

-(void) zoomInLocation: ( CLLocation * ) location
{
    shopMapView.region = MKCoordinateRegionMakeWithDistance(  location.coordinate, 800,800 );
}

我写的一个应用程序也有类似的问题。我的MKMapView始于大西洋,非洲海岸,纬度和经度为(0,0)。请尝试在需要获取用户位置的位置插入:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


       int timeout = 10000000;

       while (self.mapView.userLocation.location == nil && timeout > 0) {

          sleep(1);
          timeout--;
    }

    //do whatever you need to do once you have the user's location. 
    [self findLocation];
});

这将等待直到找到用户的位置,然后执行您需要执行的操作。它不在主线程上,因此不会冻结UI

问题是地图应该缩放到“用户的当前位置”,但findLocation中的代码将
location
设置为从Google api查询返回的一些值。将NSLogs放入findLocation以打印返回的值,并确认代码正在执行您认为应该执行的操作。还建议在调试器中单步执行该代码。还要确认findInLocation和zoomInLocation实际执行。在zoomInLocation中,NSLog坐标值。不,这只是这里的SO。有趣的是,你把这个逻辑放在代码中的什么地方?视频下载?是的,我就是在那里下载的。
-(void) zoomInLocation: ( CLLocation * ) location
{
    shopMapView.region = MKCoordinateRegionMakeWithDistance(  location.coordinate, 800,800 );
}
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


       int timeout = 10000000;

       while (self.mapView.userLocation.location == nil && timeout > 0) {

          sleep(1);
          timeout--;
    }

    //do whatever you need to do once you have the user's location. 
    [self findLocation];
});