我正在尝试使用Google Places API
获取位置并将其显示在mapView
上,我有一个名为myList的NSMutableArray
。 myList的NSLog
输出是
Project[3151:15803] mylist array (
"<ObjectInfo: 0x81aa040>",
"<ObjectInfo: 0x81aa140>",
"<ObjectInfo: 0x81aa170>",
"<ObjectInfo: 0x81aa1a0>",
"<ObjectInfo: 0x81aa1d0>",
"<ObjectInfo: 0x81a9f20>",
"<ObjectInfo: 0x81a9f50>",
"<ObjectInfo: 0x81a9f80>",
"<ObjectInfo: 0x81a9fb0>",
"<ObjectInfo: 0x81aa260>",
"<ObjectInfo: 0x81aa290>",
"<ObjectInfo: 0x81aa2c0>",
"<ObjectInfo: 0x81aa2f0>",
"<ObjectInfo: 0x81aa320>",
"<ObjectInfo: 0x81aa350>",
"<ObjectInfo: 0x81aa380>",
"<ObjectInfo: 0x81aa3b0>",
"<ObjectInfo: 0x81aa450>",
"<ObjectInfo: 0x81aa480>",
"<ObjectInfo: 0x81aa4b0>"
我试图使用[self.mapView addAnnotations:myList];
似乎我需要以某种方式访问坐标并以这种方式显示位置。我不确定。谢谢你的帮助。
这是我的代码:
- (CLLocationManager *)locationManager
{
if (locationManager != nil)
{
return locationManager;
}
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDelegate:self];
return locationManager;
}
- (void)showAnnotation
{
MKCoordinateRegion region;
region.center = theCoordinate;
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
region.span=span;
[mapView setRegion:region animated:TRUE];
DDAnnotation *annotation = [[[DDAnnotation alloc] initWithCoordinate:theCoordinate addressDictionary:nil] autorelease];
NSLog(@"mylist array %@", myList);
[self.mapView addAnnotations:myList];
}
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityIndicator setCenter:CGPointMake(160.0f, 208.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.tag = INDICATOR_VIEW;
[self.view addSubview:activityIndicator];
[activityIndicator release];
self.myTable = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain] autorelease];
[self.myTable setDelegate:self];
[self.myTable setDataSource:self];
[self.myTable setBackgroundColor:[UIColor clearColor]];
[self.myTable setRowHeight:74];
[self.myTable setSeparatorColor:[UIColor clearColor]];
//[self.myTable setScrollEnabled:NO];
// self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone; // none style
[self.view addSubview:self.myTable];
if ([ProjUtil connectedToWeb]) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // send loc updates to myself
[self.locationManager startUpdatingLocation];
//UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_VIEW];
[activityIndicator startAnimating];
[backBt setEnabled:NO];
[myTable setUserInteractionEnabled:NO ];
} else {
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"" message:@"Internet connection fails" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
[alert show];
return;
}
}
// Listen to change in the userLocation
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta = 0.05; // Change these values to change the zoom
span.longitudeDelta = 0.05;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UILabel* nameLb;
UILabel* detailsLb;
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:@"log-cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"log-cell"] autorelease];
UIImage* img = [ImgUtil image:[NSString stringWithFormat:@"butList3_%d.png", [[SelectCategory instance] select] + 1 ]];
UIImageView* bgImage = [[[UIImageView alloc] initWithImage:img ]autorelease];
[cell addSubview:bgImage ];
[cell sendSubviewToBack:bgImage ];
nameLb = [[[UILabel alloc] initWithFrame:CGRectMake(40, 5, 250, 40)] autorelease];
[nameLb setNumberOfLines:4];
[nameLb setBackgroundColor:[UIColor clearColor]];
[nameLb setTextColor:[UIColor whiteColor]];
[nameLb setTag:NAME_TEXT];
[nameLb setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
[nameLb setMinimumFontSize:10];
[cell addSubview:nameLb];
detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(40, 45, 250, 20)] autorelease];
[detailsLb setBackgroundColor:[UIColor clearColor]];
[detailsLb setTextColor:[UIColor whiteColor]];
[detailsLb setTag:DETAILS_TEXT];
[detailsLb setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[cell addSubview:detailsLb];
}else {
nameLb = (UILabel*)[cell viewWithTag:NAME_TEXT];
detailsLb = (UILabel*)[cell viewWithTag:DETAILS_TEXT];
}
ObjectInfo* obj = [myList objectAtIndex:indexPath.row ] ;
nameLb.text = [obj name];
double distance = [ProjUtil getDistanceWithLat1:currentLocation.latitude long1:currentLocation.longitude lat2:obj.location.latitude long2:obj.location.longitude];
detailsLb.text = [NSString stringWithFormat:@"%.2f miles", [ProjUtil kmToMi:distance]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundColor:[UIColor clearColor]];
///[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
MoreInfoCtrl* _ctrl = [MoreInfoCtrl make];
_ctrl.object = [myList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:_ctrl animated:YES];
}
-(void) findCodeWithCoord:(CLLocationCoordinate2D) _coord{
//[[SelectCategory instance] getCategoryText]
NSString* request = [NSString stringWithFormat:LINK,_coord.latitude , _coord.longitude , [[SelectCategory instance] getCategoryText]];
//NSLog(@"%@", request);
request = [request stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
//NSLog(@"%@", request);
NSObject* requestJSON = [ServerInfoResuest getServerInfoWithAddress:request];
NSMutableDictionary* allInfo = (NSMutableDictionary* ) requestJSON;
if ([[allInfo objectForKey:@"status"] isEqualToString:@"OK"]) {
self.myList = [ObjectParse parserManager:[allInfo objectForKey:@"results"]];
// NSLog(@"allinfo dict is %@", allInfo);
}
[myTable reloadData];
UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_VIEW];
[activityIndicator stopAnimating];
[backBt setEnabled:YES];
[myTable setUserInteractionEnabled:YES ];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//[self.delegate locationUpdate:newLocation];
currentLocation = newLocation.coordinate;
// errorLabel.text = [newLocation description];
[manager stopUpdatingLocation];
[self findCodeWithCoord:currentLocation];
[locationManager stopUpdatingLocation];
theCoordinate.latitude = newLocation.coordinate.latitude;
theCoordinate.longitude = newLocation.coordinate.longitude;
[self showAnnotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
AddressAnnotation* adr = (AddressAnnotation*) annotation;
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:adr.placeTitle];
if (!pinView)
{
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:adr.placeTitle] autorelease];
// customPinView.pinColor = MKPinAnnotationColorRed;
customPinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"wpin.png"]];
AddressAnnotation* adr = (AddressAnnotation*) annotation;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(clDetails) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
customPinView.rightCalloutAccessoryView = rightButton;
customPinView.draggable = YES;
customPinView.animatesDrop = NO;
customPinView.canShowCallout = YES;
forControlEvents:UIControlEventTouchUpInside];
// customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
return nil;
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_VIEW];
[activityIndicator stopAnimating];
[backBt setEnabled:YES];
[myTable setUserInteractionEnabled:YES ];
[locationManager stopUpdatingLocation];
}
@end
答案 0 :(得分:1)
创建注释时,应该给它一个包含纬度和经度的CLLocationCoordinate2D。你当然会从你必须解析的JSON对象中获取它。下面的行肯定是正确的,这就是你要做的所有事情,这将通过你的myList并将每个注释添加到地图视图中。确保您的注释包含经度和纬度(即坐标)
[self.mapView addAnnotations:myList];
检查下面的链接,很棒的教程。
http://iphonebyradix.blogspot.com/2011/07/working-with-google-places-api.html