2011-10-24 59 views
0

我在顯示澳大利亞大陸時啓動Map應用程序。我有一個按鈕,我想爲當前位置設置編碼,並在按下按鈕後放大到該位置。我獲得當前緯度&經度並且能夠通過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]; 

} 

(Web服務調用)

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;它會在點擊按鈕之前顯示地圖應用程序啓動時的當前位置。我很困惑以前的答案。因此,我應該在按鈕單擊事件下放置哪些代碼以顯示當前位置,並且還建議在爲Click事件提供編碼之後,是否有任何更改將當前的Lati和longi傳遞給Web服務url。在此先感謝

編輯: -

- (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); 
    } 
    } 
     } 

在搜索按鈕(調用Web服務)

 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; 



} 


} 
+0

只有在輕按按鈕或用戶位置發生變化時,您是否希望將地圖放在用戶位置上? – Anna

+0

當我打開應用程序時,我有澳大利亞大陸的mapView和覆蓋屏幕一半的一個UIview。 UIview包含以下按鈕Show Me,Radis picker,Search。所以當我點擊「顯示我」按鈕時,我想顯示用戶的當前位置。是的,我可以隱藏我的UIview來顯示整個mapView。是的,我只想在點擊按鈕時將地圖居中在用戶位置..... –

回答

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

+0

現在,當我啓動我的應用程序時,它直接打開當前位置區域。我首先想要澳大利亞大陸,然後點擊顯示我的按鈕它應該打開當前位置區域。我已經更新了我的編碼以及您的答案中的問題。你能否檢查並糾正我所缺乏的地方...... –

+0

我無法用顯示的代碼複製該問題。你是否已經實現了像didUpdateUserLocation這樣的地圖委託方法,你還要更新地圖的區域?如果是這樣,請禁用該代碼。 – Anna

+0

我已放入 - (void)viewDidload {CLLocationCoordinate2D AusLoc = {-19.048230,133。685730}; MKCoordinateSpan AusSpan = MKCoordinateSpanMake(45,45); MKCoordinateRegion AusRegion = MKCoordinateRegionMake(AusLoc,AusSpan); mapView.region = AusRegion; }另外,在我的多重註解在Map上獲得位置的地方,我已經放置了區域。 –

相關問題