Pro AddIn: Click to Open Web Page

614
12
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
1 Solution

Accepted Solutions
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!

View solution in original post

12 Replies
UmaHarano
Esri Regular Contributor

Hi Dylan

There are a few ways to accomplish this with .NET. One suggestion is posted here on stackoverflow

Code snippet to open a url in a Pro button OnClick method:

 internal class OpenURL : Button
{
protected override void OnClick()
{
System.Diagnostics.Process.Start("http://google.com");
}
}

You can use string interpolation to pass in the points to the string.  

Thanks

Uma

DylanHarwell
Occasional Contributor

Hi Uma, thanks for the reply! So when I try just that line and click the button in Pro it grays out and nothing happens. I tried a variety of the suggestions on the stackoverflow site and it's the same behavior, button grays out on first click and no browser window opens. The final suggestion seemed promising but did not work. What is commented out is what I tried just before that. 

namespace StreetViewButton
{
internal class OpenGoogle : Button
{
protected override void OnClick()
{
var processes = Process.GetProcessesByName("Chrome");
var path = processes.FirstOrDefault()?.MainModule?.FileName;
string url = "http://google.com";
//System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
//myProcess.StartInfo.UseShellExecute = true;
//myProcess.StartInfo.FileName = url;
Process.Start(path, url);
}
}
}
0 Kudos
UmaHarano
Esri Regular Contributor

Hi Dylan,

One of the main reasons this happens is if the namespace of the button class (StreetViewButton from your code snippet above) doesn't match the namespace defined in your config.daml or in your add-ins settings.

Please check out this topic to see if this could be an issue: ProGuide Diagnosing ArcGIS Pro Add ins · Esri/arcgis-pro-sdk Wiki · GitHub 

Thanks

Uma

DylanHarwell
Occasional Contributor

The config was auto generated, and I have not touched it since adding the button to the project. Looks like it added _Module, _Group1, _Button1 to the namespace name. Do these all need to be identical?

0 Kudos
DylanHarwell
Occasional Contributor

I read that wiki, not sure how to do this: 'Access your add-in project's Properties page through its context menu.'

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Dylan

In this screenshot, I see one issue: In the button element defined in the Config.daml,  notice that the className attribute is set to Button1. But in the code snippet you shared above your button class is actually OpenGoogle. So try changing that in the config.daml - to match your button class.

DylanHarwell
Occasional Contributor

That was trick! Thank you for your help, I'm such a noob at this. 

0 Kudos
DylanHarwell
Occasional Contributor

Now the next step is to have it open a link not when the user clicks the button, but when the user clicks the button and THEN clicks in the map...

0 Kudos
UmaHarano
Esri Regular Contributor

Great! Glad that worked.

0 Kudos