Pro AddIn: Click to Open Web Page

3123
13
Jump to solution
05-01-2019 10:58 AM
DylanHarwell
Occasional Contributor

I'm still bitter with ESRI for not bringing Python Add-Ins over to Pro.. I have never coded in C#/.NET and know very little about ArcObjects. My Python skills I consider to be strong, not just scripting but app development, so I think I can pick up on the .NET SDK, though I am reluctant to get into this rabbit hole. I have VS and the SDK up and running, went through some tutorials, just looking for some guidance on a specific solution here.

A while back I created a little ArcMap Add-In, just a single toolbar button that will open Google Street View in your default browser when the user clicks a location on the map. I'd like to deploy a button like this in Pro, but after searching and searching for 'C# open a web page on click event' all I was coming up with was ASP.NET stuff and I have zero knowledge on any of this stuff lol! I was basically looking for a one liner to add into the onClick event of my button.cs code to just open Google.com to start with.

Below is the Python Add-In code that accomplishes this, and I am not looking for anyone to code this entirely for me, but maybe some suggestions, guidance, resources, etc. All this does is grab the coordinates from where the user clicks and feeds that into a url to open street view.

import arcpy
import pythonaddins
import webbrowser
import threading

class OpenStreetView(object):
    """Implementation for StreetView_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 3 # cross hair cursor

    def onMouseDownMap(self, x, y, button, shift):
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        sr_df = df.spatialReference
        pt = arcpy.Point(x,y)
        pt_geom = arcpy.PointGeometry(pt, sr_df)
        new_pt_geom = pt_geom.projectAs("WGS 1984")
        new_pt = new_pt_geom.firstPoint
        new_X = new_pt.X
        new_Y = new_pt.Y
##        message = "Projected: " + str(x) + ", " + str(y) + " Geographic: " + str(new_X) + ", " + str(new_Y)
##        pythonaddins.MessageBox(message, "Coordinates")
        maps_url = "https://maps.google.com/maps?q=&layer=c&cbll=" + str(new_Y) + "," + str(new_X)
        threading.Thread(target=webbrowser.open, args=(maps_url,0)).start()
0 Kudos
13 Replies
MattWashburn1
New Contributor II

Hi Dylan,

Try creating a MapTool instead of a button. I had the exact same problem and found a solution here:

https://community.esri.com/thread/190516-is-there-a-streetview-add-in-for-arcgis-pro

One thing to keep in mind is that you need a way to deactivate the tool after you click on the map. Adding this line at the end of the QueuedTask works for me:

FrameworkApplication.SetCurrentToolAsync("esri_mapping_exploreTool");

Based on this, I was also able to recreate a python addin that opens Google Maps at the MapView center and zoom level.

Hope this helps!

DylanHarwell
Occasional Contributor

Thanks Matt, got it working in about 10 min! Good trick to change the cursor back after the click event. I noticed the url they used tells google to open the map facing either Eeast or West, I forget which, but changing the 90 to a 0 makes it open facing North.

0 Kudos
MattWashburn1
New Contributor II

Glad it's working! I noticed that URL direction too. I also changed the base URL to this format:  

"https://www.google.com/maps/@?api=1&map_action=pano&viewpoint="

Both work, but I believe this one is the more current standard:

https://developers.google.com/maps/documentation/urls/guide#street-view-action

0 Kudos
MarietteShin
New Contributor III

I just moved to ArcGIS Pro 3.1 and had to migrate my Visual Studio 2022 Google Streetview Add In from 2.x to 3.x. I used the "Pro Migration Solution" extension tool in VS, but even afterwards, although the project Built okay, the Add In failed with a system error. It turns out that the .NET Windows Desktop Runtime 6.0 (the new .NET framework), requires a change in the way that the Process.Start (url) is called. I've copied my script below, which includes the syntax used for the Process.Start call:

using System.Threading.Tasks;
using System.Diagnostics;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace StreetviewPro
{
internal class StreetviewPro : MapTool
{
protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
e.Handled = true; //Handle the event args to get the call to the corresponding async method
}

protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
{
return QueuedTask.Run(() =>
{// Convert the clicked point in client coordinates to the corresponding map coordinates.
MapPoint mapPoint = MapView.Active.ClientToMap(e.ClientPoint);// convert to lon/lat
MapPoint coords = (MapPoint)GeometryEngine.Instance.Project(mapPoint, SpatialReferences.WGS84);
// open a web browser
string url = string.Format("http://maps.google.com/?cbll={0},{1}&cbp=12,90,0,0,5&layer=c", coords.Y, coords.X);
Process.Start(new ProcessStartInfo (url) { UseShellExecute = true });
});
}
public StreetviewPro()
{
//IsSketchTool = true;
//SketchType = SketchGeometryType.Rectangle;
//SketchOutputMode = SketchOutputMode.Map;
}

protected override Task OnToolActivateAsync(bool active)
{
return base.OnToolActivateAsync(active);
}

protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
return base.OnSketchCompleteAsync(geometry);
}
}
}

 

That script made my StreetView Add In work in ArcGIS Pro 3.1

Hopefully, someone else finds this info useful 🙂