Bing Maps in iPhone Application - iphone

How to integrate Bing Maps into iPhone Application?
I am doing using VirtualEarthKit framework [website appears to host malware].
My code is as below :
I am getting error while getting token.
{
VECommonService *commonService = [[VECommonService alloc] init];
NSString *token;
printf("2\n");
NSError *error=[commonService getToken:&token forUserID:#"abc" password:#"def" ipAddress:#"123"];
NSString *errMsg= [error description];
printf("\n%s\n",[errMsg UTF8String]);
printf("3\n");
VEGeocodeService *geocodeService = [[VEGeocodeService alloc] init];
VEGeocodeRequest *geocodeRequest = [[VEGeocodeRequest alloc] init];
printf("4\n");
geocodeRequest.query = #"Portland State University";
geocodeRequest.token = token;
printf("5\n");
VEServiceResponse *geocodeResponse = [geocodeService geocode:geocodeRequest];
VEGeocodeResult *geocodeResult = [geocodeResponse.results objectAtIndex:0];
//NSLog([geocodeResult.location description]);
//NSLog([geocodeResult.address description]);
VEImageryService *imageryService = [[VEImageryService alloc] init];
VEGetMapURIRequest *mapRequest = [[VEGetMapURIRequest alloc] init];
mapRequest.center = geocodeResult.location;
mapRequest.token = token;
VEGetMapURIResponse *mapResponse = [imageryService getMapURI:mapRequest];
NSURL *url = mapResponse.mapURL;
//printf("\n\n URL==> %s",[url relativeString]);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
}
is there any way to use bing maps in an iPhone app?

Related

iphone mapview get directions

I would like to find a simple way to get directions. Is there any way to do this with mapview?
Below is my current code.
- (IBAction)goToMap:(id)sender {
[self.view addSubview:self.mapUIView];
[self.mapUIView setFrame:CGRectMake(0, 0, 320, 460)];
self.mapview.mapType = MKMapTypeStandard;
CLLocationCoordinate2D coordinate;
coordinate.latitude = [[self.find lat] floatValue];
coordinate.longitude = [[self.find lng] floatValue];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coordinate];
[annotation setTitle:[self.find name]];
//[annotation setSubtitle:#"Hello"];
[self.mapview addAnnotation:annotation];
[annotation release];
[self zoomMapViewToFitAnnotations:self.mapview animated:YES];
}
You cannot use MapKit for driving directions. It does not support this.
If you want to show the current position of device (like while driving) you have to add CoreLocation framework in your project and should use "updateLocation" method for getting current location but if you want to draw a path between two location you have to use Google Api for this.
If you want to plot directions use:
https://github.com/kishikawakatsumi/MapKit-Route-Directions
CLLocationManager *manager = [[CLLocationManager alloc] init] ;
CLLocationCoordinate2D currentLocation = [manager location].coordinate;
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(37.365502,-94.394531);
NSString *fromLocation = [NSString stringWithFormat: #"%f,%f",
currentLocation.latitude, currentLocation.longitude];
NSString *toLocation = [NSString stringWithFormat: #"%f,%f",
Coordinate.latitude, Coordinate.longitude];
NSString *url = [NSString stringWithFormat:
#"http://maps.google.com/maps?saddr=%#&daddr=%#", fromLocation, fromLocation];
UIWebView *webView = [[UIWebView alloc]init];
NSURL *targetURL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

Passing coords of annotation Mapkit tapped

In my app, I have a set of location and their relative annotation. I have a button where cliking it, i go to a method that call a URL web service to get the google street view.
My code is:
- (NSString*)urlStreetview:(id<MKAnnotation>)annotation{
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
NSLog(#"lat:%f",pinLocation.coordinate.latitude);
NSLog(#"lon:%f",pinLocation.coordinate.longitude);
NSString *lat_string = [[NSString alloc] initWithFormat:#"%f", pinLocation.coordinate.latitude];
NSString *lon_string = [[NSString alloc] initWithFormat:#"%f", pinLocation.coordinate.longitude];
NSString *url1 = [NSString stringWithFormat:#"http:myweb/streetview.php"];
NSString *url2 = [url1 stringByAppendingString: #"?lat="];
NSString *url3 = [url2 stringByAppendingString:lat_string];
NSString *url4 = [url3 stringByAppendingString:#"&lon="];
NSString *url = [url4 stringByAppendingString:lon_string];
NSLog(#"url:%#",url);
return url;
}
I wrote the previous method to have a url that is composed with the LatLong of the annotation.
I have a button called PressMe on the annotation pin. When I push it, I want to to excute the previous method, to calle the url, but I do not understand how pass the selected annotation to the streetView method.
- (IBAction)PressMe:(id)sender
{
UIViewController *webViewController = [[[UIViewController alloc] init] autorelease];
UIWebView *uiWebView = [[[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,480)] autorelease];
[uiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [self urlStreetview: ????]]]]; //HERE IS MY MY PROBLEM
[webViewController.view addSubview: uiWebView];
webViewController.title = #"web bar";
[self.navigationController pushViewController:webViewController animated:YES];
//[[self navigationController] pushViewController:thirdViewController animated:YES];
}
thanks in advance!
I am going to answer my question myself. I just resolve my problem an it work perfectly. The code allows to show a web view in order to have a steet View google service of the annotation map Kit annotation. Obviusly, as I am relatively new iphone developer,if someone wants to add/suggest/improve, he is welcome.
My button implementation:
- (IBAction)PressMe:(id)sender
{
UIViewController *webViewController = [[[UIViewController alloc] init] autorelease];
UIWebView *uiWebView = [[[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,480)] autorelease];
[uiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: self.urlStreetview]]];
[webViewController.view addSubview: uiWebView];
webViewController.title = #"web bar";
[self.navigationController pushViewController:webViewController animated:YES];
//[[self navigationController] pushViewController:thirdViewController animated:YES];
}
With this button I call my (following) method "urlStreetview" the, with the annotation selected, composes a string in order to have a complet url with the apropriete GET php parameter. The url is the direction of my (very simple) web service that allow to call the google API street View.
- (NSString*)urlStreetview{
//there can only be one selected annotation so get the one at index 0
id<MKAnnotation> selectedAnnotation = [_mapView.selectedAnnotations objectAtIndex:0];
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:selectedAnnotation.coordinate.latitude longitude:selectedAnnotation.coordinate.longitude];
//Now I have the annotation coordinate selected, so now I am able to compound the urladdingo to my base url:http://myurl/streetview.php, the GET part, I mean the lat lon coords:
NSString *lat_string = [[NSString alloc] initWithFormat:#"%f", pinLocation.coordinate.latitude];
NSString *lon_string = [[NSString alloc] initWithFormat:#"%f", pinLocation.coordinate.longitude];
NSString *url1 = [NSString stringWithFormat:#"http://myurl/streetview.php"];
NSString *url2 = [url1 stringByAppendingString: #"?lat="];
NSString *url3 = [url2 stringByAppendingString:lat_string];
NSString *url4 = [url3 stringByAppendingString:#"&lon="];
NSString *url = [url4 stringByAppendingString:lon_string];
NSLog(#"url:%#",url);
return url;
}
I hope It can be useful!

How can we handle multiple NSURLConnection in iPhone Xcode?

I am developing one small app in which i have multiple NSURLConnection.I have created that NSURL Connection but i don't know how to handle it.My code is like below.
-(void) loadTrafficAndEvent {
int a=10;
//Get the map view bounds for fetch the travel time markers from web service
MKCoordinateRegion region = mapView.region;
float print = region.center.latitude;
// NSLog(#"region.center=%g",print);
CGPoint firstcorner = CGPointMake(self.mapView.bounds.origin.x , mapView.bounds.origin.y);
CGPoint secondcorner = CGPointMake((self.mapView.bounds.origin.x+mapView.bounds.size.width) , mapView.bounds.origin.y);
CGPoint thirdcorner = CGPointMake(self.mapView.bounds.origin.x , (mapView.bounds.origin.y+ mapView.bounds.size.height));
CGPoint fourthcorner = CGPointMake((self.mapView.bounds.origin.x+mapView.bounds.size.width), (mapView.bounds.origin.y + mapView.bounds.size.height));;
//Then transform those point into lat,lng values
CLLocationCoordinate2D mapfirstcorner,mapsecondcorner,mapthirdcorner,mapfourthcorner,requestsender;
mapfirstcorner = [mapView convertPoint:firstcorner toCoordinateFromView:mapView];
mapsecondcorner = [mapView convertPoint:secondcorner toCoordinateFromView:mapView];
mapthirdcorner = [mapView convertPoint:thirdcorner toCoordinateFromView:mapView];
mapfourthcorner = [mapView convertPoint:fourthcorner toCoordinateFromView:mapView];
NSDateFormatter *dateFormatter;
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSString *date = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
NSString *checksumString = [NSString stringWithFormat:#"TrafficAndEvents%#ForTravelStar", date];
NSString *md5Checksum = [self getMD5CheckSum:checksumString];
NSString *url = [NSString stringWithFormat:#"http://www.travelstar.nl/travelstarwebservice/ProviderServices.asmx/GetTrafficStatusAndEvent?northWestLatitude=%f&northWestLongitude=%f&southEastLatitude=%f&southEastLongitude=%f&zoomLevel=%d&date=%#&checksum=%#", mapfirstcorner.latitude,mapfirstcorner.longitude, self.mapView.region.center.latitude, self.mapView.region.center.longitude, a,date,md5Checksum];
NSString *url1 = [NSString stringWithFormat:#"http://www.travelstar.nl/travelstarwebservice/ProviderServices.asmx/GetTrafficStatusAndEvent?northWestLatitude=%f&northWestLongitude=%f&southEastLatitude=%f&southEastLongitude=%f&zoomLevel=%d&date=%#&checksum=%#", mapsecondcorner.latitude,mapsecondcorner.longitude, self.mapView.region.center.latitude, self.mapView.region.center.longitude, a,date,md5Checksum];
NSString *url2 = [NSString stringWithFormat:#"http://www.travelstar.nl/travelstarwebservice/ProviderServices.asmx/GetTrafficStatusAndEvent?northWestLatitude=%f&northWestLongitude=%f&southEastLatitude=%f&southEastLongitude=%f&zoomLevel=%d&date=%#&checksum=%#", mapthirdcorner.latitude,mapthirdcorner.longitude, self.mapView.region.center.latitude, self.mapView.region.center.longitude, a,date,md5Checksum];
NSString *url3 = [NSString stringWithFormat:#"http://www.travelstar.nl/travelstarwebservice/ProviderServices.asmx/GetTrafficStatusAndEvent?northWestLatitude=%f&northWestLongitude=%f&southEastLatitude=%f&southEastLongitude=%f&zoomLevel=%d&date=%#&checksum=%#", mapfourthcorner.latitude,mapfourthcorner.longitude, self.mapView.region.center.latitude, self.mapView.region.center.longitude, a,date,md5Checksum];
//Release the request if it is already created.
if(request1 ) {
[request release];
request = nil;
}
else if(request1 ) {
[request1 release];
request1 = nil;
}
else if(request2 ) {
[request2 release];
request2 = nil;
}
else if(request3 ) {
[request3 release];
request3 = nil;
}
//Release the connection if it is already created.
if(conn) {
[conn cancel];
[conn release];
conn = nil;
}
else if(conn1) {
[conn1 cancel];
[conn1 release];
conn1 = nil;
}
else if(conn2) {
[conn2 cancel];
[conn2 release];
conn2 = nil;
}
else if(conn3) {
[conn3 cancel];
[conn3 release];
conn3 = nil;
}
//If zoom level is grater then 6 then it will request for fetch the travel time markers from the web servce.
if(a > 6) {
ZOOM_LEVEL = a;
//Create the request for fetch the data from web service.
request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
request1 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url1]];
request2 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url2]];
request3 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url3]];
//NSLog(#"%#",url);
//NSLog(#"Request sent");
//entryDate = [NSDate date];
//[entryDate retain];
//Create the connection with the web service for fetch the data
// DownloadDelegate *dd = [[DownloadDelegate alloc] init];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
conn1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
conn2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self];
conn3 = [[NSURLConnection alloc] initWithRequest:request3 delegate:self];
}
}
Declare conn,conn1,conn2,conn3 in .h file.
Then do the following.
in loadTrafficAndEvent:
conn1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
in connectionDidFinishDownloading: method,
- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL{
if(connection==conn){
conn1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
}
else if(connection==conn1){
conn2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self];
}
else if(connection==conn2){
conn3 = [[NSURLConnection alloc] initWithRequest:request3 delegate:self];
}
}
Do the operations inside each if else condition, and no need to allocate and initialize all NSURLConnection in loadTrafficAndEvent: The download will occur one after other.
you might want to take a look at AFNetworking for an easier and tidier way of doing network requests.
Implement NSURLConnection delegate methods and rest will be handle by itself

Making a call programmatically from iPhone app and returning back to the app after ending the call

I am trying to initiate a call from my iphone app,and I did it the following way..
-(IBAction) call:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Call Besito" message:#"\n\n\n"
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
NSString *phone_number = #"0911234567"; // assing dynamically from your code
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", phone_number]]];
NSString *phone_number = #"09008934848";
NSString *phoneStr = [[NSString alloc] initWithFormat:#"tel:%#",phone_number];
NSURL *phoneURL = [[NSURL alloc] initWithString:phoneStr];
[[UIApplication sharedApplication] openURL:phoneURL];
[phoneURL release];
[phoneStr release];
}
}
by the above code..I am able to successfully make a call..but when I end a call,I'm not able to return to my app
So, I want to know how to achieve,that..also please tell me how we can initiate a call using webview...
This is my code :
NSURL *url = [NSURL URLWithString:#"telprompt://123-4567-890"];
[[UIApplication sharedApplication] openURL:url];
Use this so that after call end it will return to app.
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:#"tel:+9196815*****"];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
Please try this it will definitely let you Back to Your App.
NSString *phoneNumber = // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
following code will not return to your app [no alertview will show before make call]
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#",phoneNumber]];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
following code will return to your app "alertview will show before make call"
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", phoneNumber]];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
You can also use webview, this is my code :
NSURL *url = [NSURL URLWithString:#"tel://123-4567-890"];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 150, 100)];
[self.view addSubview:btn];
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(50, 50, 150, 100)];
webview.alpha = 0.0;
[webview loadRequest:[NSURLRequest requestWithURL:url]];
// Assume we are in a view controller and have access to self.view
[self.view insertSubview:webview belowSubview:btn];
[webview release];
[btn release];
It's not possible to return back to the app after ending a call because it's an app.

Get Twitter oauth key in similar way to Facebook oauth key

I have an iPhone app which currently loads up a UIWebView using the facebook graph api oauth url (https://graph.facebook.com/oauth/authorize?...) - a user then enters their login details, and on submitting I retrieve the the access token from the URL. (In a manner as this I believe http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api) This is then passed to my java server which performs the various actions using RestFB
My question is, is there a similar way to do this with Twitter? Currently it seems the only way to get the data is to download a library and use that; yet this is an approach I would rather not use as it would take up unnecessary space seeing as all the commands will actually be run from my Java server.
Any advice would be greatly appreciated
Thanks,
Dan
Twitter uses OAuth 1.0a authentication, but not OAuth 2.0.
Steps to get an access token on Twitter:
Create your own application on Twitter and get consumer key and secret.
Download open source OAuth library, OAuthConsumer in my case.
Write registration code, see example below.
p.s. Set application type as web application, it turns on callback instead of PIN code.
static NSString* kMyApplicationConsumerKey = #"XXXXXXXXXXX";
static NSString* kMyApplicationConsumerSecret = #"XXXXXXXXXXX";
#interface TwitterConnector : NSObject {
OAConsumer* consumer;
OAToken* requestToken;
OAToken* accessToken;
}
- (void)start;
#end
#implementation TwitterConnector
- (void)start {
consumer = [[OAConsumer alloc] initWithKey:kMyApplicationConsumerKey secret:kMyApplicationConsumerSecret];
NSURL* requestTokenUrl = [NSURL URLWithString:#"http://api.twitter.com/oauth/request_token"];
OAMutableURLRequest* requestTokenRequest = [[[OAMutableURLRequest alloc] initWithURL:requestTokenUrl
consumer:consumer
token:nil
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* callbackParam = [[[OARequestParameter alloc] initWithName:#"oauth_callback" value:#"twitter://authorized"] autorelease];
[requestTokenRequest setHTTPMethod:#"POST"];
[requestTokenRequest setParameters:[NSArray arrayWithObject:callbackParam]];
OADataFetcher* dataFetcher = [[[OADataFetcher alloc] init] autorelease];
[dataFetcher fetchDataWithRequest:requestTokenRequest
delegate:self
didFinishSelector:#selector(didReceiveRequestToken:data:)
didFailSelector:#selector(didFailOAuth:error:)];
}
- (void)didReceiveRequestToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
requestToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSURL* authorizeUrl = [NSURL URLWithString:#"https://api.twitter.com/oauth/authorize"];
OAMutableURLRequest* authorizeRequest = [[[OAMutableURLRequest alloc] initWithURL:authorizeUrl
consumer:nil
token:nil
realm:nil
signatureProvider:nil] autorelease];
NSString* oauthToken = requestToken.key;
OARequestParameter* oauthTokenParam = [[[OARequestParameter alloc] initWithName:#"oauth_token" value:oauthToken] autorelease];
[authorizeRequest setParameters:[NSArray arrayWithObject:oauthTokenParam]];
UIWebView* webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[[[UIApplication sharedApplication] keyWindow] addSubview:webView];
[webView release];
webView.delegate = self;
[webView loadRequest:authorizeRequest];
}
- (void)didReceiveAccessToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
accessToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
// FINISHED!
}
- (void)didFailOAuth:(OAServiceTicket*)ticket error:(NSError*)error {
// ERROR!
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqualToString:#"twitter"]) {
// Extract oauth_verifier from URL query
NSString* verifier = nil;
NSArray* urlParams = [[[request URL] query] componentsSeparatedByString:#"&"];
for (NSString* param in urlParams) {
NSArray* keyValue = [param componentsSeparatedByString:#"="];
NSString* key = [keyValue objectAtIndex:0];
if ([key isEqualToString:#"oauth_verifier"]) {
verifier = [keyValue objectAtIndex:1];
break;
}
}
if (verifier) {
NSURL* accessTokenUrl = [NSURL URLWithString:#"https://api.twitter.com/oauth/access_token"];
OAMutableURLRequest* accessTokenRequest = [[[OAMutableURLRequest alloc] initWithURL:accessTokenUrl
consumer:consumer
token:requestToken
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* verifierParam = [[[OARequestParameter alloc] initWithName:#"oauth_verifier" value:verifier] autorelease];
[accessTokenRequest setHTTPMethod:#"POST"];
[accessTokenRequest setParameters:[NSArray arrayWithObject:verifierParam]];
OADataFetcher* dataFetcher = [[[OADataFetcher alloc] init] autorelease];
[dataFetcher fetchDataWithRequest:accessTokenRequest
delegate:self
didFinishSelector:#selector(didReceiveAccessToken:data:)
didFailSelector:#selector(didFailOAuth:error:)];
} else {
// ERROR!
}
[webView removeFromSuperview];
return NO;
}
return YES;
}
- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
// ERROR!
}
#end