2011-10-24 16 views
0

私の地図アプリケーションが起動すると、オーストラリアの大陸が表示されます。私は現在の場所のコーディングを設定し、バトンを押した後にその場所を拡大するボタンを1つ持っています。現在の緯度が&になり、WebサービスのURLを渡すことができます。私はMapViewのを設定するとクリックイベントの現在の場所のクエリ

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

    } 

:私は

コーディングで更新
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]; 

を検索]ボタン(Webサービス呼び出し)の

- (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]; 

} 

コーディング以下でした。 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); 
    } 
    } 
     } 
(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

私はアプリケーションを開くとき、私はaustralia大陸と画面の半分をカバーする1つのUIviewとmapViewを持っています。 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]; 
} 

012:ユーザの場所に地図の中心又は領域を設定します didUpdateToLocationメソッドでは、 "currentLocation"変数を設定していますが、必要はありません。 locationManagerにはすでにlocationというプロパティがあります。

いずれにしても、絶対有効緯度または経度が「有効」であるとみなすことは正しくありません。なぜなら、a)赤道または本緯線付近の位置を除外しているからです。 b)それ以外の場合は、newLocationnilかどうかを確認することをお勧めします。

+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; }また、私の複数の注釈がマップ上に配置されるところで、私は領域を配置しました。 –

関連する問題