Iphone 谷歌地图没有显示所需的位置

Iphone 谷歌地图没有显示所需的位置,iphone,objective-c,Iphone,Objective C,我在iPhone应用程序中实现谷歌地图。当我使用文本字段搜索特定城市时,谷歌地图会在该城市设置动画片刻,然后移回我的当前位置。我不知道为什么会这样,所以请告诉我我需要做什么才能得到想要的位置 这是我已经实现的代码 -(IBAction)changeMapType:(id)sender{ seg = (UISegmentedControl*)sender; if (seg.selectedSegmentIndex == 0) { mMapView.mapType =

我在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);
    }
-(iAction)changeMapType:(id)发送方{
seg=(UISegmentedControl*)发送方;
如果(seg.selectedSegmentIndex==0){
mMapView.mapType=MKMapTypeStandard;
}
else if(seg.selectedSegmentIndex==1){
mMapView.mapType=mkmaptypestellite;
}
}
#pragma标记注释回调
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
NSLog(@“这被称为”);
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“customloc”];
[annView setPinColor:MKPinAnnotationColorPurple];
返回视图;
}
#pragma标记位置回调
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
NSLog(@“找到位置…正在更新区域”);
[self addPins:newLocation.coordinate.latitude lon:newLocation.coordinate.longitude];
}
-(无效)locationManager:(CLLocationManager*)manager错误:(N错误*)错误{
NSLog(@“位置不可用”);
}
#pragma标记地理函数
-(空)添加引脚:(浮动)横向长度:(浮动)纵向长度{
CLLOCATION坐标2D定位;
位置纬度=纬度;
location.longitude=lon;
//你周围的穹窿
协调区域;
区域。中心=位置;
Mk坐标跨度;
跨度.纬度德尔塔=0.5f;
span.纵向德尔塔=0.5f;
地区span=span;
[mMapView setRegion:区域动画:TRUE];
float westLon=region.center.longitude-region.span.longitudeDelta;
浮动南纬=region.center.lation-region.span.latitudeDelta;
//随机填充屏幕->这应转换为数据库驱动的坐标
位置。纬度=南纬+(region.span.latitudelta/50.0f)*(arc4random()%100);
location.longitude=westLon+(region.span.longitudeDelta/50.0f)*(arc4random()%100);
//添加自定义位置标记
CustomPlaceMark*placemark=[[CustomPlaceMark alloc]initWithCoordinate:location];
placemark.title=@“此处的标题”;
placemark.subtitle=@“subtitle Here”;
[mMapView addAnnotation:placemark];
[placemark release];
}
-(CLLocationCoordinate2D)getLocationFromAddress:(NSString*)地址{
//如果出现错误,请使用api密钥,如
// http://maps.google.com/maps/geo?q=%@&output=csv&key=yourgelmapsapikey
NSString*urlString=[NSString stringWithFormat:@]http://maps.google.com/maps/geo?q=%@&输出=csv“,[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray*listItems=[locationString组件由字符串分隔:@“,”];
双纬度=0.0;
双经度=0.0;
如果([listItems count]>=4&&[listItems objectAtIndex:0]IsequalString:@“200”]){
纬度=[[listItems对象索引:2]双值];
经度=[[listItems对象索引:3]doubleValue];
}
否则{
//显示错误
NSLog(@“错误:找不到地址”);
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“错误”消息:@“未找到地址”
代表:自我取消按钮:@“确定”其他按钮:无];
[警报显示];
[警报发布];
}
CLLOCATION坐标2D定位;
位置。纬度=纬度;
location.longitude=经度;
返回位置;
}
#pragma标记搜索委托
-(iAction)按钮单击:(id)发件人
{
CLLocationCoordinate2D location2d=[self-getLocationFromAddress:textfield.text];
[self addPins:location2d.latitude lon:location2d.longitude];
NSLog(@“位置纬度=%f”,位置2D.纬度);
NSLog(@“位置纬度=%f”,位置2D.经度);
}

在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;
}

谢谢亲爱的朋友,它工作得很好,你能告诉我我在哪里出错吗:)很乐意帮助。。。didUpdateToLocation是CLLocationManager类的一个委托,该委托为您提供设备的GPS位置,因为它会被反复调用,因此您的函数addPins会反复聚焦到您的当前位置。哦,但这里产生的另一个问题是,在添加代码之前,它向我显示了iphone地图而不是google地图。我可以看到google地图,所以你能告诉我如何看到google地图吗?它的怪异行为不应该是这样的,可能是这次你在ios5.1或更高版本中运行该项目。