gpt4 book ai didi

ios - 连接到服务器然后在 mkmapview 上绘制坐标

转载 作者:行者123 更新时间:2023-11-29 02:31:52 25 4
gpt4 key购买 nike

我的问题相当简单,但我似乎无法弄清楚。

我正在尝试向我的 map View 添加一个刷新按钮,如果有任何更新,该按钮将返回到服务器并检索新位置。我下面的代码已经可以使用 viewdidload 方法第一次调用服务器,并且可以将服务器上的所有位置绘制到 map 上。我现在需要的是一个按钮,无论何时按下它都会发出相同的调用,我正在为我的所有代码使用一个类,因此非常感谢您提供可以轻松与代码合并的最简单的解决方案。

我对 ios 编程也很陌生,因此任何有关如何整理我的代码的建议也将不胜感激。

这是我的 View Controller ,其中包含我的所有代码。

//ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {

[self.mapView setShowsUserLocation:YES];

} else {
[locationManager requestWhenInUseAuthorization];
}

NSURL *jsonFileUrl = [NSURL URLWithString:@"http://sample.name/service.php"];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}

#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
_downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_downloadedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

NSMutableArray *_locations = [[NSMutableArray alloc] init];

NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

CLLocationCoordinate2D coordinate;

for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];

MKPointAnnotation* marker = [[MKPointAnnotation alloc] init];

marker.title = jsonElement[@"Name"];
marker.subtitle = jsonElement[@"Address"];
coordinate.latitude = [jsonElement [@"Latitude"] doubleValue];
coordinate.longitude = [jsonElement [@"Longitude"] doubleValue];

marker.coordinate = coordinate;
[_locations addObject:marker];
}

[self.mapView addAnnotations:_locations];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier;
{
if (annotation == mapView.userLocation) return nil;

MKAnnotationView *annotationView;
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"blue_pin.png"];
} else {
annotationView.annotation = annotation;
}
return annotationView;
}

return nil;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.mapView setShowsUserLocation:YES];
}
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D myLocation = [userLocation coordinate];
MKCoordinateRegion zoomRegion = MKCoordinateRegionMakeWithDistance(myLocation, 10000, 10000);
[self.mapView setRegion:zoomRegion animated:YES];
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)InterfaceOrientation
{
return (InterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)Refresh:(id)sender {
}
@end

最佳答案

您可以通过评论找到我的解决方案。

刷新或收到响应时删除旧数据。

- (IBAction)action_goToManageDevice:(id)sender
{
[self reloadData];
}

- (void)reloadData
{
// Remove old annotation
[self.mapView removeAnnotations:self.mapView.annotations];

// reload your data
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://sample.name/service.php"];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}

关于ios - 连接到服务器然后在 mkmapview 上绘制坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26757485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com