0

I implement Google Maps in my iPhone app. When I search for particular city using a textfield Google Maps animates to that city for a moment and then moves back to my current location. I don't know why its happing so please tell me what I need to do to get the desired location.

This is the code which I have implemented

-(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);
    }
adnan
  • 1
  • 1
  • 4

1 Answers1

1

In the CLLocationManager delegate

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

Comment the line [self addPins:newLocation.coordinate.latitude lon:newLocation.coordinate.longitude]; as it is zooming to your current location.

or in your -(void)addPins:(float)lat lon:(float)lon function execute the following line for only once with the help of a 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;
}
superGokuN
  • 1,399
  • 13
  • 28
  • thanks dear friend its working well can you tell me where i am doing mistake – adnan Jan 02 '13 at 14:46
  • :) happy to help... didUpdateToLocation is a Delegate of CLLocationManager class, this delegate gives you the GPS location of the Device, as it is called repeatedly so as your function addPins thus map is repeatedly focusing to your current location. https://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/doc/uid/TP40007125 – superGokuN Jan 02 '13 at 14:51
  • https://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/doc/uid/TP40007124 – superGokuN Jan 02 '13 at 14:51
  • oh but another problem created here because it show me the iphone map not the google map before adding your code i can see google map so can you tell me how i can see google map – adnan Jan 02 '13 at 14:53
  • It's weird shouldn't behave like this, may be this time you are running the project in ios5.1 or later. – superGokuN Jan 02 '13 at 14:55
  • i am running it on ios6 mate client requirement is to implement app using google map not the iphone map – adnan Jan 02 '13 at 14:57
  • you can see on getlocationfromaddress method i im appending the value with google link – adnan Jan 02 '13 at 15:05
  • http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html#//apple_ref/doc/uid/TP40009497-CH4-SW5 – superGokuN Jan 02 '13 at 15:21