简体   繁体   中英

How to load address in webview in iPhone?

Currently i am working in iPhone application, Using UIWebview to load address like "America", then run the application, the map not shown on the screen. I want to shown the location from User enter search bar field, How to fix this iuissue? please help me

Thanks in Advance

I tried this:

    webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 40, 320,376)];
    webView.delegate=self;
    webView.scalesPageToFit=YES;
    NSString *mapurl = @"http://maps.google.com/maps?q=";
    NSString *urlString1 = [mapurl stringByAppendingFormat:@"America"];    
    NSString *OmitSpace = [urlString1 stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

    NSURL *url=[NSURL URLWithString:OmitSpace];
    NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

Screen shot: 在此处输入图片说明

Another solution is to use googleapis's to find the latitude and longitude to the api's JSON response and use them in MKMapView to show that particular region..

An example..

http://maps.googleapis.com/maps/api/geocode/json?address=america&sensor=true

The result is JSON output where you can then fetch the Latitude and Longitude,further subjecting it to the MKMapView

or for iOS 5 and above use CLGeocoder and MKMapView to get the same output

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:@"America" completionHandler:^(NSArray *placemarks, NSError *error) {
        //Error checking

        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        MKCoordinateRegion region;
        region.center.latitude = placemark.region.center.latitude;
        region.center.longitude = placemark.region.center.longitude;
        MKCoordinateSpan span;
        double radius = placemark.region.radius / 1000; // convert to km

        NSLog(@"Radius is %f", radius);
        span.latitudeDelta = radius / 112.0;

        region.span = span;

        [mapView setRegion:region animated:YES];
    }];
NSString *OmitSpace = [urlString1 stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

This will omit the whitespace only. It will not convert other escape characters. So replace above line with this.

NSString *OmitSpace = [urlString1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM