2011-10-24 13 views
0

내지도 응용 프로그램을 시작할 때 호주 대륙을 보여주고 있습니다. 나는 현재 위치에 대한 코딩을 설정하고 부툰을 누른 후 해당 위치를 확대하고자하는 버튼을 하나 가지고 있습니다. 현재 위도가 &이고 웹 서비스 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]; 
} 

    } 

나는지도보기를 설정합니다. showUserLocation = YES; 지도 응용 프로그램이 시작될 때 현재 위치를 보여줍니다. 이전 답변과 혼동 스럽습니다. 그래서 현재 위치를 보여주기 위해 버튼 클릭 이벤트 아래에 어떤 코드를 넣어야하는지 그리고 클릭 이벤트 코딩을 한 후 웹 서비스 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); 
    } 
    } 
     } 

(호출 웹 서비스) 당신의 버튼을 탭 방법에서

 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 버튼이 있습니다. 그래서 Show me 버튼을 누르면 현재 사용자의 위치를 ​​보여주고 싶습니다. 그리고 네, 전체 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

이제 내 응용 프로그램을 시작할 때 현재 위치 영역과 함께 직접 열립니다. 나는 호주 대륙을 먼저 원한다. Show me 버튼을 누르면 현재 위치가 열린다. 나는 당신의 대답으로 질문뿐만 아니라 나의 코딩을 업데이트했다. 내가 부족한 부분을 확인하고 교정 할 수 있습니까? –

+0

코드에 표시된 문제를 복제 할 수 없습니다. didUpdateUserLocation과 같은지도 대리자 메소드를 구현하여지도의 지역도 업데이트 했습니까? 그렇다면 해당 코드를 비활성화하십시오. – Anna

+0

나는 - (무효) viewDidload {CLLocationCoordinate2D AusLoc = {-19.048230,133.685730}; MKCoordinateSpan AusSpan = MKCoordinateSpanMake (45, 45); MKCoordinateRegion AusRegion = MKCoordinateRegionMake (AusLoc, AusSpan); mapView.region = AusRegion; } 또한지도에서 여러 개의 주석이 표시되는 곳에 영역을 배치했습니다. –