Bing Map Layer

Download Sample Application

Bing map layers display map imagery from Microsoft's Bing services. Click on the buttons to change the Bing map styles to different values such as Aerial, Road and Hybrid. To use the layer you must initialize it with your Bing key.

@property (strong) AGSBingMapLayer *bingMapLayer;

//create the bing map layer and provide the appId (the unique Bing key)
self.bingMapLayer = [[AGSBingMapLayer alloc] initWithAppID:appId style:AGSBingMapLayerStyleAerial];
self.bingMapLayer.delegate = self;

//add the bing map layer to the mapView
[self.mapView addMapLayer:self.bingMapLayer withName:@"Bing Map Layer"];

Sample Code

//SWIFT SAMPLE CODE
/*
Copyright 2014 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import Cocoa
import ArcGIS

class BingMapLayerSwiftSample: NSViewController, AGSLayerDelegate {

    @IBOutlet weak var mapView:AGSMapView!
    @IBOutlet weak var segmentedControl:NSSegmentedControl!
    @IBOutlet weak var textField:NSTextField!
    var bingMapLayer:AGSBingMapLayer!
    
    //MARK: - Awake From Nib
    
    // -------------------------------------------------------------------------------
    //  awakeFromNib
    // -------------------------------------------------------------------------------
    override func awakeFromNib() {
    
        //if bing map's key is provided then load layer
        if self.textField.stringValue != "" {
            self.addBingMapLayerForAppID(self.textField.stringValue)
        }
        else {
            self.segmentedControl.enabled = false
        }
    }
    
    //MARK: - Helper Message
    
    // -------------------------------------------------------------------------------
    //	addBingMapLayerForAppID:appId
    // -------------------------------------------------------------------------------
    func addBingMapLayerForAppID(appId:String) {
    
        //reset mapView
        self.mapView.reset()
        
        //add bing map layer
        self.bingMapLayer = AGSBingMapLayer(appID: appId, style: .Aerial)
        self.bingMapLayer.delegate = self
        self.mapView.addMapLayer(self.bingMapLayer, withName:"Bing Map Layer")
    }
    
    //MARK: - Action Method
    
    // -------------------------------------------------------------------------------
    //  toggleBingMapLayerStyle:sender
    // -------------------------------------------------------------------------------
    @IBAction func toggleBingMapLayerStyle(sender:NSSegmentedControl) {
        
        //set bing map's style based on user selection
        if sender.selectedSegment == 0 {
            self.bingMapLayer.style = .Aerial
        }
        else if sender.selectedSegment == 1 {
            self.bingMapLayer.style = .Road
        }
        else if sender.selectedSegment == 2 {
            self.bingMapLayer.style = .AerialWithLabels
        }
    }
    
    //MARK: - Layer Delegate
    
    // -------------------------------------------------------------------------------
    //  layerDidLoad:layer
    // -------------------------------------------------------------------------------
    func layerDidLoad(layer: AGSLayer!) {
        println("Layer \(layer.name) loaded..")
        self.segmentedControl.enabled = true
    }
    
    // -------------------------------------------------------------------------------
    //  layer:didFailToLoadWithError:error
    // -------------------------------------------------------------------------------
    func layer(layer: AGSLayer!, didFailToLoadWithError error: NSError!) {
        self.segmentedControl.enabled = false
        if let viewWindow = self.view.window {
            let alert = NSAlert()
            alert.messageText = "Layer '\(layer.name)' failed to load"
            alert.informativeText = error.localizedDescription
            alert.beginSheetModalForWindow(viewWindow, modalDelegate:self, didEndSelector:nil, contextInfo:nil)
        }
    }
    
    //MARK: - NSTextField Notification
    
    // -------------------------------------------------------------------------------
    //  controlTextDidEndEditing:aNotification
    //
    //  this will be called when user finish entering bing map's key
    // -------------------------------------------------------------------------------
    override func controlTextDidEndEditing(aNotification: NSNotification) {
        
        //select bing map's style
        self.segmentedControl.selectedSegment = 0
        
        //load bing map's layer with provided bing map's key
        if self.textField.stringValue != "" {
            self.addBingMapLayerForAppID((aNotification.object as NSTextField).stringValue)
        }
        else {//else remove bing map's layer
            self.mapView.removeMapLayer(self.bingMapLayer)
        }
    }
    
}
//OBJECTIVE C SAMPLE CODE
/*
 Copyright 2013 Esri
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import "BingMapLayerSample.h"

@interface BingMapLayerSample ()

@end

@implementation BingMapLayerSample

#pragma mark - Awake From Nib

// -------------------------------------------------------------------------------
//  awakeFromNib
// -------------------------------------------------------------------------------
- (void)awakeFromNib {
    
    //if bing map's key is provided then load layer
    if (self.textField.stringValue && [self.textField.stringValue isNotEqualTo:@""]) {
        [self addBingMapLayerForAppID:self.textField.stringValue];
    }
}

#pragma mark - Helper Message

// -------------------------------------------------------------------------------
//	addBingMapLayerForAppID:appId
// -------------------------------------------------------------------------------
- (void)addBingMapLayerForAppID:(NSString*)appId {
    
    //reset mapView
    [self.mapView reset];
    
    //add bing map layer
    self.bingMapLayer = [[AGSBingMapLayer alloc] initWithAppID:appId style:AGSBingMapLayerStyleAerial];
    self.bingMapLayer.delegate = self;
    [self.mapView addMapLayer:self.bingMapLayer withName:@"Bing Map Layer"];
}

#pragma mark - Action Method

// -------------------------------------------------------------------------------
//  toggleBingMapLayerStyle:sender
// -------------------------------------------------------------------------------
- (IBAction)toggleBingMapLayerStyle:(id)sender {
    
    //set bing map's style based on user selection
    if ([sender selectedSegment] == 0) {
        self.bingMapLayer.style = AGSBingMapLayerStyleAerial;
    }
    else if ([sender selectedSegment] == 1) {
        self.bingMapLayer.style = AGSBingMapLayerStyleRoad;
    }
    else if ([sender selectedSegment] == 2) {
        self.bingMapLayer.style = AGSBingMapLayerStyleAerialWithLabels;
    }
}

#pragma mark - Layer Delegate

// -------------------------------------------------------------------------------
//  layerDidLoad:layer
// -------------------------------------------------------------------------------
- (void)layerDidLoad:(AGSLayer *)layer {
    NSLog(@"Layer %@ loaded..",layer.name);
}

// -------------------------------------------------------------------------------
//  layer:didFailToLoadWithError:error
// -------------------------------------------------------------------------------
- (void)layer:(AGSLayer *)layer didFailToLoadWithError:(NSError *)error {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:[NSString stringWithFormat:@"Layer '%@' failed to load",layer.name]];
    [alert setInformativeText:[NSString stringWithFormat:@"%@",error]];
    [alert beginSheetModalForWindow:self.view.window modalDelegate:self didEndSelector:nil contextInfo:nil];
}

#pragma mark - NSTextField Notification

// -------------------------------------------------------------------------------
//  controlTextDidEndEditing:aNotification
//
//  this will be called when user finish entering bing map's key
// -------------------------------------------------------------------------------
- (void)controlTextDidEndEditing:(NSNotification *)aNotification {
    
    //select bing map's style
    [self.segmentedControl setSelectedSegment:0];
    
    //load bing map's layer with provided bing map's key
    if (self.textField.stringValue && [self.textField.stringValue isNotEqualTo:@""]) {
        [self addBingMapLayerForAppID:[NSString stringWithFormat:@"%@",((NSTextField*)aNotification.object).stringValue]];
    }
    else {//else remove bing map's layer
        [self.mapView removeMapLayer:self.bingMapLayer];
    }
}

@end
//OBJECTIVE C SAMPLE CODE
/*
 Copyright 2013 Esri
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import <Cocoa/Cocoa.h>

@interface BingMapLayerSample : NSViewController <AGSLayerDelegate>

@property (strong) IBOutlet AGSMapView *mapView;
@property (strong) IBOutlet NSSegmentedControl *segmentedControl;
@property (strong) IBOutlet NSTextField *textField;
@property (strong) AGSBingMapLayer *bingMapLayer;

- (IBAction)toggleBingMapLayerStyle:(id)sender;

@end
Feedback on this topic?