将GPS坐标从我的iPhone传递到Web服务,然后在地图上显示GPS坐标

问题描述:

我正在尝试创建一个应用程序,该应用程序可以获取iPhone的GPS坐标并在地图上显示我的当前位置.

I am trying to create an App which takes the GPS co-ordinates of my iPhone and displays my current position on the Map.

我已经使用LocateMe示例代码从我的iPhone中获取了GPS坐标,所以现在这对我来说不是问题.

I have got the GPS co-ordinates from my iPhone using LocateMe Sample code, so now that is not a problem for me.

但是我不知道如何将我的GPS坐标从iPhone发送到网络服务,以及如何在地图上显示GPS坐标.如果你们能帮助我解决这个问题,那就太好了.

But I don't know how to send my GPS co-ordinates from my iPhone to a web service and display the GPS co-ordinates on a Map. It would be nice if you guys could help me out with this issue.

您可能需要按以下方式使用nsurlconnection类(您只需要在NSURLRequest中设置requestWithUrl参数即可,该参数是Web服务特定的url):

You'll probably want to use the nsurlconnection class as follows (you'll just need to set the requestWithUrl parameter in the NSURLRequest which the web-service specific url):

-(void)getLocationsFromWebService {
    NSLog(@"in getLocationsFromWebService");


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];


    NSURLRequest *theRequest = [NSURLRequest requestWithURL:self.locationsURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    self.requestType = @"GET";
    if (theConnection) {
        _responseData = [[NSMutableData data] retain ];
    } else {

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; 
    NSLog(@"in mapviewcontroller");
    NSLog(@"Error connecting - %@",[error localizedDescription]);
    [connection release];
    [_responseData release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (404 == statusCode || 500 == statusCode) {
        //[self.controller setTitle:@"Error Getting Parking Spot ....."];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];

        [connection cancel];
        NSLog(@"Server Error - %@", [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
    } else {
        if ([self.requestType isEqualToString:@"GET"])
            [_responseData setLength:0];
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if ([self.requestType isEqualToString:@"GET"])
        [_responseData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {

    if ([self.requestType isEqualToString:@"GET"]) {
        [self parseLocations:_responseData];
        [_responseData release];
    }
    [connection release];

}