While in drafting stage this post was originally called “ArcGIS Silverlight API: Adding Google Streets” but the only thing ArcGIS Silverlight API adds to the mix is the extraction of the coordinates from the map. For a regular Silverlight application just pass the coordinates from another source. For a version with Google Map API V3 see part 2.
My team leader asked me to try and add Google Street View to our map (that uses ArcGIS Silverlight API). The idea that he wanted me to look at was simple: when the user right-clicks on the map a new window will open with Google Street View. There was just a minor problem with this – Israel doesn’t have a Google Street View yet and our development environment is using the map of Israel .
But it was minor because I just downloaded ESRI’s Interactive Samples and worked on those. I decided to work on the simplest of the samples – Mapping –> ArcGIS Tiled Layer:

(for a live demo of the sample click here)
The code is in ArcGISSilverlightSDK\Map\Map.xaml and just contains:
- <UserControl x:Class="ArcGISSilverlightSDK.Map"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
- <Grid x:Name="LayoutRoot" >
-
- <esri:Map x:Name="MyMap">
- <esri:ArcGISTiledMapServiceLayer ID="MyLayer"
- Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
- </esri:Map>
-
- </Grid>
- </UserControl>
End result:

(that after clicking on London)
Most of the code here uses a sample I found here (it’s not in English, without explanations, but it’s mostly code so I managed).
Changes to the html/aspx file:
- <body>
- <div id="DllShepherd_GoogleStreetViewContainer" style="position:absolute;z-index:10;bottom:0px;right:0px; height: 325px; width: 300px;background-color:#888888;display:none;">
- <input type="button" style="position:relative;z-index:1000;height: 25px; width: 60px;left:180px;top:5px"
- value="Close" onclick="document.getElementById('DllShepherd_GoogleStreetViewContainer').style.display='none';"/>
- <div id="DllShepherd_GoogleStreetviewObject" style="position:relative;height: 290px; width: 290px;left:5px;top:5px"></div>
- </div>
- <form id="form2" runat="server" style="height:100%">
- <div id="silverlightControlHost">
- <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
- <param name="source" value="ClientBin/DllShepherd.SilverlightGoogleMaps.xap"/>
- <param name="onError" value="onSilverlightError" />
- <param name="background" value="white" />
- <param name="minRuntimeVersion" value="4.0.50826.0" />
- <param name="autoUpgrade" value="true" />
- <param name="windowless" value="true" />
- <asp:Literal ID="ParamInitParams" runat= "server"></asp:Literal>
- <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none">
- <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
- </a>
- </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
- </form>
- </body>
- </html>
(the sample used JavaScript to dynamically create multiply Google Street View containers, I only need one)
Added the GoogleStreeViewContainer and the most important part is this change to the Silverlight object:
- <param name="windowless" value="true" />
Without it the Silverlight object will always be on top (took me a few hours since the original sample was with the Silverlight asp control).
- <script src="http://maps.google.com/maps?file=api&v=2&key=YourGoogleApiKey" type="text/javascript"></script>
- <script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(document).ready(function () {
- pan = new GStreetviewPanorama(document.getElementById("DllShepherd_GoogleStreetviewObject"));
- GEvent.addListener(pan, "error", StreetViewError);
- });
-
- var pan;
-
- //Edited original sample (simplified)
- function streetView(lat, lon) {
-
- var latlng = new GLatLng(lat, lon);
- var svp = new GStreetviewClient();
- svp.getNearestPanoramaLatLng(latlng,
- function (newPoint) {
- if (newPoint == null) {
- //Street View Not Available For This Location (alerts just annoy me)
- return;
- }
- document.getElementById("DllShepherd_GoogleStreetViewContainer").style.display = 'block';
- panoramaOptions = { latlng: newPoint };
- var myPov = { yaw: 180, pitch: 0 };
- pan.setLocationAndPOV(latlng, myPov);
-
- });
- }
-
- //Taken from sample
- function StreetViewError(errorCode) {
- if (errorCode == 603) {
- alert("Error: Flash doesn't appear to be supported by your browser");
- return;
- }
- if (errorCode == 600) {
- alert("Street View Not Available For This Location");
- return;
- }
- }
- </script>
Added some JavaScript that adds the flash object to the Div tag GoogleStreeViewObject.
Be sure to change the Google Map API Key (YourGoogleApiKey) in:
- <script src="http://maps.google.com/maps?file=api&v=2&key=YourGoogleApiKey"
Without the API key the only place this site will work will be in localhost. The key is domain specific but the FAQ provides a way to concatenate several API keys.
You can get a key from Google Map API V2 site. Before registering be sure to read this FAQ first, will help you in choosing the best base site so that all the sub domains will be valid.
From the Silverlight application calling the JavaScript function StreetView:
- private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
- {
- var p = e.MapPoint;
- HtmlPage.Window.Invoke("streetView", p.Y, p.X);
- }
(in a general Silverlight application just pass the coordinate here)
Since it’s a simple sample I just changed the map’s url and added a code behind mouse click event. The reason I changed the map is that Google Street View expects a spatial reference of Geographic WGS84 (see my other post Geographic Coordinates Systems) which the original map didn’t have (in a real application you will either use this coordinate or will have to convert the coordinates).
- <esri:Map x:Name="MyMap" MouseClick="MyMap_MouseClick">
- <esri:ArcGISTiledMapServiceLayer ID="MyLayer"
- Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
- </esri:Map>
And that’s it.
Code sample can be found in my CodePlex Project, or directly here.
Edit: Added a part on the Google Map API v2 Key.
Edit (13/09/2011): Fixed Firefox bug in JavaScript
Continued in part 2.
Resources:
Geocoding & StreetView Google per ESRI API Silverlight – not in English but it’s mostly code…
ESRI’s Interactive Samples
Keywords: ESRI, Silverlight, Map, Google Street View