开发者

Passing GPS coordinates from my iPhone to a web service and displaying the GPS coordinates on a Map

开发者 https://www.devze.com 2023-03-20 02:25 出处:网络
I am trying to开发者_Go百科 create an App which takes the GPS co-ordinates of my iPhone and displays my current position on the Map.

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

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

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.


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];

}


You need not to create web services for just to display your current location on Map. You just need to implement corelocation and Mapkit framework in order to achieve your functionality. As you said you got your GPS co-ordinates so you just need to implement mapkit and set your current location as user location. Map kit does it automatically so you need not to do anything for that. Just search for mapkit example and you are ready to go.


Yo need to:

Use NRURLRequest in order to pass the latitude/longitude (using a POST method for example) to a PHP page.

Create that PHP scipt in order to read that parameters and then build location like this: location => NAME, lat=>LATITUDE ,lon =>LONGITUDE and use a jsonphp method to build that in json format.

in iphone, now you just receive this information using NSURLRequest and parse the json. send the result of parse to the mapkit.

0

精彩评论

暂无评论...
验证码 换一张
取 消