Developing Virtual Earth iPhone Applications with Objective-C

Developing Virtual Earth iPhone Applications with Objective-C

Rate This
  • Comments (12)

image You’ve been waiting for this post. If you’ve tried doing any iPhone development with Virtual Earth you’ve probably been swearing up a storm, beating down your computer, and lost hours of sleep wondering how Loopt, Weather Central and now NMobile got their iPhone apps working. Well, Colin Cornaby from Consonance Software may just be your new best friend. He’s the one who build the NMobile application I posted to the blog yesterday - “Mobile Speed Trap Mapping with iPhone” – which uses 100% supported code from Microsoft Virtual Earth to get the road and aerial map tiles via the Virtual Earth Web Services and use them for your iPhone applications.

image

Before we begin I want to address the challenges so you have an appreciation for just how HUGE this it.

  • First, you’ll have to write against the Virtual Earth Web Service which communicates using SOAP XML, so you can’t use JavaScript. Um, ok.
  • Next, Apple forgot to include iPhone support for SOAP (oops), so you’ll have to create request packets and parse response packets to and from the service, respectively.
  • Then, with the Virtual Earth Imagery Service you only get a single quad key back (a part of the tile URL); and, with that you need put together the entire tile URL.
  • Next, you’ll have to reconstruct the Virtual Earth tile pyramid extrapolated out from the quad key returned from VEWS. The tile pyramid schema is documented in the MSDN Technical Article, “Virtual Earth Tile System.”
  • Finally, you’ll have to put it all together into a pretty iPhone-ish interface.

Wow! This sounds treacherous (but very doable). And, well, it would be treacherous but there’s always someone out there who has the drive to make stuff happen. Colin from Consonance just so happens to be that someone you’ve been looking for. Ready?

Consonance Software has created the VirtualEarthKit for ObjC / Cocoa developers. The VirtualEarthKit contains a set of APIs that you can reference for your iPhone projects to develop iPhone applications using Microsoft Virtual Earth. So, download the VirtualEarthKit (in SVN Repository | trunk | English.lproj | InfoPlist.strings), add a reference to the library from your project, then access the VirtualEarthKit APIs which automatically wrap the Virtual Earth Web Service requests and parse the responses for your application. The VirtualEarthKit supports many of the VEWS features, in addition to getting maps, including geocoding, reverse geocoding, adding pushpins and user profile elements (device type and distance unit enumerations).

Here’s a little sample to whet you Cocoa / ObjC devs on how to access VE tiles:

//
//  VEImageryService.m
//  MapView
//
//  Created by Colin Cornaby on 10/12/08.
//  Copyright 2008 __Consonance Software__. All rights reserved.
//

#import "VEImageryService.h"
#import "VEImageryResult.h"
#import "VEGetMapURIResponse.h"
#import "VEServicePrivate.h"

@implementation VEImageryService

-(VEServiceResponse *)getImageryMetadata:(VEImageryMetadataRequest *)request
{
    xmlNodePtr xmlRequest;
    [request serializeToXMLQuery:&xmlRequest];
    NSString *server = @"
http://dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc";
    if(self.realm==kVEStagingRealm)
        server = @"
http://staging.dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc";
    xmlNodePtr result = [super sendVERequestWithBody:xmlRequest action:@"http://dev.virtualearth.net/webservices/v1/geocode/contracts/IImageryService/GetImageryMetadata" server:[NSURL URLWithString:server] error:nil];
    xmlNodePtr body = nil;
    for(body = result->children; body; body = body->next)
    {
        if(!strcmp((char *)body->name, "GetImageryMetadataResult"))
            break;
    }
    VEServiceResponse *response = nil;
    if(body)
        response = [[VEServiceResponse alloc] initWithXMLNode:body resultClass:[VEImageryResult class]];
    return response;
}

-(VEGetMapURIResponse *)getMapURI:(VEGetMapURIRequest *)request
{
    xmlNodePtr xmlRequest;
    [request serializeToXMLQuery:&xmlRequest];
    NSString *server = @"
http://dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc";
    if(self.realm==kVEStagingRealm)
        server = @"
http://staging.dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc";
    xmlNodePtr result = [super sendVERequestWithBody:xmlRequest action:@"http://dev.virtualearth.net/webservices/v1/imagery/contracts/IImageryService/GetMapUri" server:[NSURL URLWithString:server] error:nil];
    xmlNodePtr body = nil;
    for(body = result->children; body; body = body->next)
    {
        if(!strcmp((char *)body->name, "GetMapUriResult"))
            break;
    }
    VEGetMapURIResponse *response = nil;
    if(body)
        response = [[VEGetMapURIResponse alloc] initWithXMLNode:body resultClass:[VEImageryResult class]];
    return response;
}

@end

A little about the VirtualEarthKit from the Consonance web site:

VirtualEarthKit is a framework to allow Cocoa developers to communicate with Microsoft Virtual Earth. Microsoft Virtual Earth provides a wide range of services, including geocoding, reverse geocoding, map imagery, route guidance, and business lookup.

Dependencies for VirtualEarthKit have also been minimized in order to keep VirtualEarthKit portable for different OS X platforms. VirtualEarthKit uses LibXML for constructing all SOAP requests. To facilitate clean integration with Cocoa and the iPhone SDK, VirtualEarthKit requires the CoreLocation framework. Fortunately, VirtualEarthKit for Mac OS X uses Philippe Casgrain's re-implementation of CoreLocation for Mac OS X. Integration with CoreLocation allows developers for the iPhone platform to use VirtualEarthKit cleanly with the phone's GPS implementation.

Okay, so this is just game changing for mobile mapping. iPhone has completely taken off and I receive inquiries every day for iPhone developer support and licensing. Well, let’s get developing! Can adding maps to your iPhone app get any easier now? If you still need a license, contact me so we can get that out of the way. Download the VirtualEarthKit now and get cranking!

CP

Join Bing Community
  • Great work guys. I hope you get to create some cool apps.

  • This is awesome, functionality of this sort should be useful to any developer using location based services (no need to exit your .app to get useful mapping anymore!).  

    However, it is still a bit daunting for developers new to the platform -- is the image above compiled from the code snippet here?  If possible, I'd love to take a look at the whole code used to build that image - it looks like many elements that would be useful to developers trying to paste this into apps are already present and perhaps the code may be useful in its entirety?  If this is possible, it would be greatly appreciated!

  • This is just a snippet. You can get the all of the code @ http://svn.consonancesw.com/vekit/trunk/.

    CP

  • I agree, this is awesome! Thank you.

    Quick question, the sample desktop app seems to retrieve a static image. When folks have implemented this on the iPhone did they have to calculate which tiles to fetch next, or did I miss that functionality in the VEKit?

    Thanks again!

  • They may be calculating which tiles to call next on the client.

    CP

  • I have tried using this framework for the iPhone but I've had trouble when creating my project. I get this error.

    dyld: Library not loaded: /Users/ryan/Library/Frameworks/VirtualEarthKit.framework/Versions/A/VirtualEarthKit

     Referenced from: /Users/ryan/Library/Application Support/iPhone Simulator/User/Applications/7E47FE9A-FFFA-4C0B-ADE2-CFE4382C6673/Hello.app/Hello

     Reason: image not found

    Any ideas someone might be able to help me with?

    Thanks,

    Ryan

  • Am I understanding this right? This is just an API to access the maps service but there is no iphone control like the one built in the iphone sdk for google maps yet. Are you planning to provide such a control in the nearer future? And for which platforms? The point is - why should one use virtualearth when he gets google maps on the silverplate?

  • Hi,

    I would like to ask you guys if someone help me,

    I dont understand how you use it in our iPhone application,

    is there any documentation of sample we can have ?

    Thanks

    Naren

    narender.mudgal@gmail.com

  • Hi,

    i think this page is missing some care taker,

    no one is replying and i stucked at same place,

    some times it is showing libxml , corelocation, virtualearthkit framework image not found

    i dont understand what is going on. no one wants to discuss about it.

    if there is other discussion page please let me know

    i want to discuss about it , i wasted near about 10 hours on it,

    and got no ideas how to use it in iphone projects.

    please some one here reply.

  • Naren,

    There is no native support for iPhone and Bing Maps. Anything you see on this blog is considered unsupported code and meant to provide guidance, but not support. If you're interested in leveraging Bing Maps for iPhone you can either post questions on the developer forum and/or email our friends at Apple and tell them you want native support for Bing Maps.

    CP

  • Daniel,

    Correct. There is no Bing Maps iPhone control / library. Some open source folks have created some libs that are useful, but nothing Microsoft supported. Also, as for using Google over Bing on iPhone, well that is a tough sell if all you're developing is an iPhone app; however, most of our partners/customers are building out more than just iPhone specific apps and Bing Maps scales out to more devices allowing access to the tile sets via a web service. This would allow them to reuse the code they write against Bing Maps for use on iPhone, WAP sites, Windows Mobile, etc. So, yeah, the story with Bing Maps and iPhone is pretty weak right now, but trust me I'm working on it.

    CP

  • Bing is  a  really  overlord!!    support Bing~~

Page 1 of 1 (12 items)