MKMapView with address

后端 未结 5 1902
梦毁少年i
梦毁少年i 2021-02-05 15:25

is there a way to make the MKMapView place a pin with a given address? Without using the Coordinates

Thanks

5条回答
  •  广开言路
    2021-02-05 16:07

    here is a snippet of code I used in an application

    -(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
        NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                               [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr]];
        NSArray *items = [locationStr componentsSeparatedByString:@","];
    
        double lat = 0.0;
        double lon = 0.0;
    
        if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
            lat = [[items objectAtIndex:2] doubleValue];
            lon = [[items objectAtIndex:3] doubleValue];
        }
        else {
            NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
        }
        CLLocationCoordinate2D location;
        location.latitude = lat;
        location.longitude = lon;
    
        return location;
    }
    

提交回复
热议问题