点击事件的当前位置查询

时间:2011-10-24 11:28:59

标签: iphone annotations

我在地图应用程序启动时正在显示澳大利亚大陆。我有一个按钮,我想在其上设置当前位置的编码,并在按下按钮后放大到该位置。我正在获得当前的纬度和经度并且能够传递Web服务URL。我做了以下编码

   - (void)viewDidLoad 
  { 

//to set the australia region
CLLocationCoordinate2D AusLoc = {-19.048230,133.685730};
MKCoordinateSpan AusSpan = MKCoordinateSpanMake(45, 45);
MKCoordinateRegion AusRegion = MKCoordinateRegionMake(AusLoc, AusSpan);

mapView.region = AusRegion;




    locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation]; }

   - (void)locationManager:(CLLocationManager *)manager
 didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
  {

if((fabs(newLocation.coordinate.latitude) > 0.001) || (fabs(newLocation.coordinate.longitude) > 0.001)) {
    NSLog(@"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    if (currentLocation != nil) {
        [currentLocation release];
    }
    currentLocation = newLocation;
    [currentLocation retain];
}
NSString *lati=[NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
NSString *longi=[NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];

}

搜索按钮(网络服务呼叫)

NSString *url = [NSString stringWithFormat:@"http://.....url...../hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];

更新了编码:

    - (IBAction) showAddress {

    if (locationManager.location == nil)
{
    NSLog(@"user location not found yet or service disabled/denied");
}
else
{



    // Change map region using span (degrees)...
    MKCoordinateSpan span = MKCoordinateSpanMake(0.001, 0.001);
    MKCoordinateRegion region = MKCoordinateRegionMake
    (locationManager.location.coordinate, span);
    [mapView setRegion:region animated:YES];
}

   }

当我设置mapView.showUserLocation = YES时;它显示在点击按钮之前地图应用程序启动时的当前位置。我对之前的回答感到困惑。那么我应该在按钮点击事件下放置什么代码来显示当前位置,并建议如果在为点击事件编码后,在Web服务URL中传递当前Lati和longi有任何更改。提前致谢

编辑: -

   - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id   <MKAnnotation>) annotation
   {
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]   initWithAnnotation:annotation reuseIdentifier:@""];
annView.pinColor = MKPinAnnotationColorGreen;
//annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
annView.enabled = YES;
annView.image = [UIImage imageNamed:@"flagg.png"];

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
                action:@selector(showDetails:)
      forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton;    

NSLog(@"Created annotation at: %f %f",    ((CustomPlacemark*)annotation).coordinate.latitude, ((CustomPlacemark*)annotation).coordinate.longitude);

[annView addObserver:self
          forKeyPath:@"selected"
             options:NSKeyValueObservingOptionNew
             context:@"GMAP_ANNOTATION_SELECTED"];

[annView autorelease];

return annView;
  }

  //return nil;


    - (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context{

  NSString *action = (NSString*)context;

// We only want to zoom to location when the annotation is actaully selected. This   will trigger also for when it's deselected
if([[change valueForKey:@"new"] intValue] == 1 && [action   isEqualToString:@"GMAP_ANNOTATION_SELECTED"]) 
{
    if([((MKAnnotationView*) object).annotation isKindOfClass:[CustomPlacemark   class]])
    {
        CustomPlacemark *place = ((MKAnnotationView*) object).annotation;

        // Zoom into the location       
        [mapView setRegion:place.coordinateRegion animated:TRUE];
        NSLog(@"annotation selected: %f %f", ((MKAnnotationView*) object).annotation.coordinate.latitude, ((MKAnnotationView*) object).annotation.coordinate.longitude);
    }
    }
      }

在“搜索”按钮(调用webservice)

     CLLocationCoordinate2D location;
        //currentLocation = newLocation;

              float radius = [[arrayNo objectAtIndex:[pickerView   selectedRowInComponent:0]] floatValue];
    //NSURL *url = [[NSURL alloc] initWithString:@"http://www.hettich.com.au/hespdirectory/phpsqlsearch_genxml.php?lat=-33.853468&lng=150.94042160000004&radius=5"];

   NSString *url = [NSString   stringWithFormat:@"http://www.hettich.com.au/hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];
//NSLog(@"NSString *url");
NSLog(@"%@", url);
   NSURL *URL = [NSURL URLWithString:url];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
  [xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)

{       resultButton.userInteractionEnabled = YES;
          //    NSMutableArray* annotations = [[NSMutableArray alloc] init];

    for (int i = 0; i < [appDelegate.markers count]; i++)
{

    marker *aMarker = [appDelegate.markers objectAtIndex:i];
    location.latitude = [aMarker.lat floatValue];
    location.longitude =[aMarker.lng floatValue];
    AddressAnnotation *annob = [[AddressAnnotation alloc] initWithCoordinate:location];
    annob.title = aMarker.name;
    annob.subTitle = aMarker.address;
    [mapView addAnnotation:annob];
    [annob release];


    CLLocationCoordinate2D ausLoc = {location.latitude,location.longitude};         //for zoom in the showroom results region
    MKCoordinateSpan ausSpan = MKCoordinateSpanMake(0.108889, 0.169922);
    MKCoordinateRegion ausRegion = MKCoordinateRegionMake(ausLoc, ausSpan);
    NSLog(@"No Errors");
    mapView.region = ausRegion;



}


}

1 个答案:

答案 0 :(得分:0)

在按钮点按的方法中,您应首先检查位置管理员是否已获得位置,然后将地图的中心或区域设置为用户的位置:

if (locationManager.location == nil)
{
    NSLog(@"user location not found yet or service disabled/denied");
}
else
{
    // Change map center (preserving current zoom level)...
    [mapView setCenterCoordinate:locationManager.location.coordinate animated:YES];

    // --OR--

    // Change map region using distance (meters)...
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance
                           (locationManager.location.coordinate, 1000, 1000);
    [mapView setRegion:region animated:YES];

    // --OR--

    // Change map region using span (degrees)...
    MKCoordinateSpan span = MKCoordinateSpanMake(0.001, 0.001);
    MKCoordinateRegion region = MKCoordinateRegionMake
                           (locationManager.location.coordinate, span);
    [mapView setRegion:region animated:YES];
}


didUpdateToLocation方法中,您正在设置“currentLocation”变量,但我认为不需要它。 locationManager已经有location属性。

在任何情况下,只考虑绝对纬度或大于0.001的经度为“有效”是不正确的,因为a)它排除了赤道或本初子午线附近的位置(即使你只对澳大利亚感兴趣,这种逻辑是错误的方式去),和b)最好检查newLocation本身是nil