In my application i want to update the MapView periodically and redraw the the path on map as per new location. Is it possible using MKMapView?
这是我在绘制地图和发布地图时用上的有效载荷。
- (void)viewDidLoad
{
NSString *url=[NSString stringWithFormat:@"xxxxx.php?uid=%@&buddy_uid=%@",UserUID,buddyUid];
NSLog(@"%@",url);
NSLog(@"%@",url);
NSString * jsonString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"return data %@",jsonString);
NSDictionary *jsonDic = [jsonString JSONValue];
buddyLocation = [[NSString alloc] initWithFormat:@"%@,%@",[jsonDic objectForKey:@"Lattitude"],[jsonDic objectForKey:@"Longitude"]];
[super viewDidLoad];
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
//[locmanager startUpdatingLocation];
MKMapView *mapnew;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
mapnew = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
}
else
{
mapnew = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
}
[mapnew setDelegate:self];
[self.view addSubview:mapnew];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = lat1 floatValue];//22.56574;
region.center.longitude =lng1 floatValue];//73.74575;
NSString *currentLocation = [[NSString alloc] initWithFormat:@"%@,%@",lat1,lng1];
NSLog(@"%@",currentLocation);
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"Current Location";
ann.coordinate = region.center;
[mapnew addAnnotation:ann];
NSLog(@"%@",jsonDic);
region.center.latitude = [[jsonDic objectForKey:@"Lattitude"] floatValue];//23.56574;
region.center.longitude = [[jsonDic objectForKey:@"Longitude"] floatValue];//72.74575;
ann = [[DisplayMap alloc] init];
ann.title = @"Buddy Location";
ann.coordinate = region.center;
[mapnew addAnnotation:ann];
KMLParser *kml = [KMLParser parseKMLAtURL:[NSURL URLWithString:[NSString
stringWithFormat:@"https://maps.google.com/maps?saddr=%@&daddr=%@&
output=kml",currentLocation,buddyLocation]]];
NSArray *overlays = [kml overlays];
[mapnew addOverlays:overlays];
MKMapRect flyTo = MKMapRectNull;
for (id <MKOverlay> overlay in overlays)
{
if (MKMapRectIsNull(flyTo))
{
flyTo = [overlay boundingMapRect];
}
else
{
flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
}
}
mapnew.visibleMapRect = flyTo;
[mapnew release];
mapnew =nil;
}
}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *line = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
line.strokeColor = [UIColor blueColor];
line.lineWidth =2.0;
return line;
}