0

我在我的 iPhone 应用程序中实现了谷歌地图。当我使用文本字段搜索特定城市时,谷歌地图会动画到该城市片刻,然后移回我当前的位置。我不知道为什么会这样,所以请告诉我需要做什么才能获得所需的位置。

这是我已经实现的代码

-(IBAction)changeMapType:(id)sender{
    seg = (UISegmentedControl*)sender;
    if (seg.selectedSegmentIndex == 0) {
        mMapView.mapType = MKMapTypeStandard;
    }
    else if (seg.selectedSegmentIndex == 1) {
        mMapView.mapType = MKMapTypeSatellite;
    }

}


#pragma mark annotation callbacks

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    NSLog(@"This is called");
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"customloc"];
    [annView setPinColor:MKPinAnnotationColorPurple];
    return annView;
}

#pragma mark location callbacks
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSLog(@"location found... updating region");
    [self addPins:newLocation.coordinate.latitude lon:newLocation.coordinate.longitude];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"location not available");
}


#pragma mark geo functions

-(void)addPins:(float)lat lon:(float)lon{

    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = lon;

    // forcus around you
    MKCoordinateRegion region;
    region.center=location;
    MKCoordinateSpan span;
    span.latitudeDelta=0.5f;
    span.longitudeDelta=0.5f;
    region.span=span;
    [mMapView setRegion:region animated:TRUE];


    float westLon = region.center.longitude - region.span.longitudeDelta;

    float southLat = region.center.latitude - region.span.latitudeDelta;


        // random fill the screen -> this should convert to database driven coordinates
        location.latitude=southLat + (region.span.latitudeDelta/50.0f)*(arc4random()%100);
        location.longitude=westLon + (region.span.longitudeDelta/50.0f)*(arc4random()%100);

        // add custom place mark
        CustomPlaceMark *placemark=[[CustomPlaceMark alloc] initWithCoordinate:location];
        placemark.title = @"Title Here";
        placemark.subtitle = @"Subtitle Here";
        [mMapView addAnnotation:placemark];
        [placemark release];


 }

    -(CLLocationCoordinate2D) getLocationFromAddress:(NSString*) address {
        // in case of error use api key like
        // http://maps.google.com/maps/geo?q=%@&output=csv&key=YourGoogleMapsAPIKey
        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
        NSArray *listItems = [locationString componentsSeparatedByString:@","];
        double latitude = 0.0;
        double longitude = 0.0;



        if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
            latitude = [[listItems objectAtIndex:2] doubleValue];
            longitude = [[listItems objectAtIndex:3] doubleValue];
        }
        else {
            //Show error
            NSLog(@"error:address not found");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Address not found"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }

        CLLocationCoordinate2D location;
        location.latitude = latitude;
        location.longitude = longitude;

        return location;
    }

    #pragma mark search delegate
    - (IBAction)buttonclick:(id)sender
    {

        CLLocationCoordinate2D location2d = [self getLocationFromAddress:textfield.text];
        [self addPins:location2d.latitude lon:location2d.longitude];
        NSLog(@"location latitude  = %f",location2d.latitude);
        NSLog(@"location latitude  = %f",location2d.longitude);
    }
4

1 回答 1

1

在 CLLocationManager 委托中

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"location found... updating region");
//[self addPins:newLocation.coordinate.latitude lon:newLocation.coordinate.longitude];
}

注释行[self addPins:newLocation.coordinate.latitude lon:newLocation.coordinate.longitude]; 因为它正在缩放到您当前的位置。

或在您的-(void)addPins:(float)lat lon:(float)lon函数中,在 Bool 的帮助下仅执行一次以下行

if(!executeOnce) // executeOnce is a BOOL
{

MKCoordinateRegion region;
region.center=location;
MKCoordinateSpan span;
span.latitudeDelta=0.5f;
span.longitudeDelta=0.5f;
region.span=span;
[mMapView setRegion:region animated:TRUE];

executeOnce = YES;
}
于 2013-01-02T14:40:12.640 回答