Skip to content

Commit 0598f4f

Browse files
authored
Merge pull request #388 from PaulGibbs3rd/master
Added geocode from a button sample in pro sdk
2 parents 4fcb1d5 + f55b221 commit 0598f4f

20 files changed

+415
-0
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"Version": 1,
3+
"WorkspaceRootPath": "C:\\Users\\pau11750\\source\\repos\\geocode from a button\\",
4+
"Documents": [
5+
{
6+
"AbsoluteMoniker": "D:0:0:{3DB7B833-10B3-46F2-B4DB-55A796702502}|geocode from a button\\geocode from a button.csproj|c:\\users\\pau11750\\source\\repos\\geocode from a button\\geocode from a button\\button1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
7+
"RelativeMoniker": "D:0:0:{3DB7B833-10B3-46F2-B4DB-55A796702502}|geocode from a button\\geocode from a button.csproj|solutionrelative:geocode from a button\\button1.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
8+
},
9+
{
10+
"AbsoluteMoniker": "D:0:0:{3DB7B833-10B3-46F2-B4DB-55A796702502}|geocode from a button\\geocode from a button.csproj|c:\\users\\pau11750\\source\\repos\\geocode from a button\\geocode from a button\\config.daml||{FA3CD31E-987B-443A-9B81-186104E8DAC1}",
11+
"RelativeMoniker": "D:0:0:{3DB7B833-10B3-46F2-B4DB-55A796702502}|geocode from a button\\geocode from a button.csproj|solutionrelative:geocode from a button\\config.daml||{FA3CD31E-987B-443A-9B81-186104E8DAC1}"
12+
}
13+
],
14+
"DocumentGroupContainers": [
15+
{
16+
"Orientation": 0,
17+
"VerticalTabListWidth": 256,
18+
"DocumentGroups": [
19+
{
20+
"DockedWidth": 200,
21+
"SelectedChildIndex": 1,
22+
"Children": [
23+
{
24+
"$type": "Bookmark",
25+
"Name": "ST:0:0:{aa2115a1-9712-457b-9047-dbb71ca2cdd2}"
26+
},
27+
{
28+
"$type": "Document",
29+
"DocumentIndex": 0,
30+
"Title": "Button1.cs",
31+
"DocumentMoniker": "C:\\Users\\pau11750\\source\\repos\\geocode from a button\\geocode from a button\\Button1.cs",
32+
"RelativeDocumentMoniker": "geocode from a button\\Button1.cs",
33+
"ToolTip": "C:\\Users\\pau11750\\source\\repos\\geocode from a button\\geocode from a button\\Button1.cs",
34+
"RelativeToolTip": "geocode from a button\\Button1.cs",
35+
"ViewState": "AQIAAAAAAAAAAAAAAAAAABIAAABoAAAA",
36+
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
37+
"WhenOpened": "2024-10-22T20:32:30.568Z",
38+
"EditorCaption": ""
39+
},
40+
{
41+
"$type": "Document",
42+
"DocumentIndex": 1,
43+
"Title": "Config.daml",
44+
"DocumentMoniker": "C:\\Users\\pau11750\\source\\repos\\geocode from a button\\geocode from a button\\Config.daml",
45+
"RelativeDocumentMoniker": "geocode from a button\\Config.daml",
46+
"ToolTip": "C:\\Users\\pau11750\\source\\repos\\geocode from a button\\geocode from a button\\Config.daml",
47+
"RelativeToolTip": "geocode from a button\\Config.daml",
48+
"ViewState": "AQIAAAYAAAAAAAAAAAAwwAsAAAAOAAAA",
49+
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.001001|",
50+
"WhenOpened": "2024-10-22T20:32:06.483Z"
51+
}
52+
]
53+
}
54+
]
55+
}
56+
]
57+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Geocode from a button
2+
3+
<!-- TODO: Write a brief abstract explaining this sample -->
4+
This sample demonstrates how to makes an http rest call to the geocoding service api via a pro sdk button. The request is sent off, response is handled, parsed, then added to the map as a graphic.
5+
6+
<!-- TODO: Fill this section below with metadata about this sample-->
7+
```
8+
Language: C#
9+
Subject: Framework
10+
Date: 10/28/2024
11+
ArcGIS Pro: 3.3.1
12+
Visual Studio: 2022
13+
.NET Target Framework: 8.0
14+
```
15+
16+
## Resources
17+
18+
* [API Reference online](https://pro.arcgis.com/en/pro-app/sdk/api-reference)
19+
* <a href="https://pro.arcgis.com/en/pro-app/sdk/" target="_blank">ArcGIS Pro SDK for .NET (pro.arcgis.com)</a>
20+
* [arcgis-pro-sdk-community-samples](https://github.com/Esri/arcgis-pro-sdk-community-samples)
21+
* [ArcGIS Pro DAML ID Reference](https://github.com/Esri/arcgis-pro-sdk/wiki/ArcGIS-Pro-DAML-ID-Reference)
22+
* [FAQ](https://github.com/Esri/arcgis-pro-sdk/wiki/FAQ)
23+
24+
25+
<!-- TODO: Explain how this sample can be used. To use images in this section, create the image file in your sample project's screenshots folder. Use relative url to link to this image using this syntax: ![My sample Image](FacePage/SampleImage.png) -->
26+
## How to use the sample
27+
28+
1. Download the sample and open geocode from a button sln
29+
2. Run the visual studio project
30+
3. Within ArcGIS Pro click on the new button that appears within the add-in tab.
31+
4. Enter an address within the new window that pops up to geocode
32+
5. Observe results within the map.
33+
34+
35+
<!-- End -->
36+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="https://esri.github.io/arcgis-pro-sdk/images/ArcGISPro.png" alt="ArcGIS Pro SDK for Microsoft .NET Framework" height = "20" width = "20" align="top" >
37+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
38+
[Home](https://github.com/Esri/arcgis-pro-sdk/wiki) | <a href="https://pro.arcgis.com/en/pro-app/sdk/api-reference" target="_blank">API Reference</a> | [Requirements](https://github.com/Esri/arcgis-pro-sdk/wiki#requirements) | [Download](https://github.com/Esri/arcgis-pro-sdk/wiki#installing-arcgis-pro-sdk-for-net) | <a href="https://github.com/esri/arcgis-pro-sdk-community-samples" target="_blank">Samples</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.34916.146
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "geocode from a button", "geocode from a button\geocode from a button.csproj", "{3DB7B833-10B3-46F2-B4DB-55A796702502}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3DB7B833-10B3-46F2-B4DB-55A796702502}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3DB7B833-10B3-46F2-B4DB-55A796702502}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3DB7B833-10B3-46F2-B4DB-55A796702502}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3DB7B833-10B3-46F2-B4DB-55A796702502}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {3440EF27-1DD3-42AC-B988-34CA9900060A}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using ArcGIS.Core.Geometry;
2+
using ArcGIS.Core.CIM;
3+
using ArcGIS.Desktop.Framework;
4+
using ArcGIS.Desktop.Framework.Contracts;
5+
using ArcGIS.Desktop.Framework.Threading.Tasks;
6+
using ArcGIS.Desktop.Mapping;
7+
using Newtonsoft.Json.Linq;
8+
using System;
9+
using System.Net.Http;
10+
using System.Threading.Tasks;
11+
using System.Windows;
12+
using ArcGIS.Core.Internal.Geometry;
13+
14+
namespace geocode_from_a_button
15+
{
16+
internal class Button1 : Button
17+
{
18+
private const string GeocodingServiceUrl =
19+
"https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates";
20+
21+
protected override async void OnClick()
22+
{
23+
try
24+
{
25+
string address = PromptForAddress();
26+
if (string.IsNullOrEmpty(address)) return;
27+
28+
await QueuedTask.Run(async () =>
29+
{
30+
var location = await GeocodeAddressAsync(address);
31+
if (location != null)
32+
{
33+
AddPointToMap(location);
34+
}
35+
});
36+
}
37+
catch (Exception ex)
38+
{
39+
MessageBox.Show($"Error: {ex.Message}");
40+
}
41+
}
42+
43+
private string PromptForAddress()
44+
{
45+
return Microsoft.VisualBasic.Interaction.InputBox(
46+
"Enter an address to geocode:",
47+
"Geocode Address",
48+
"380 New York St, Redlands, CA");
49+
}
50+
51+
private async Task<MapPoint> GeocodeAddressAsync(string address)
52+
{
53+
using (HttpClient client = new HttpClient())
54+
{
55+
string requestUrl = $"{GeocodingServiceUrl}?f=json&SingleLine={Uri.EscapeDataString(address)}&outFields=*&maxLocations=1";
56+
HttpResponseMessage response = await client.GetAsync(requestUrl);
57+
response.EnsureSuccessStatusCode();
58+
59+
string json = await response.Content.ReadAsStringAsync();
60+
JObject parsedJson = JObject.Parse(json);
61+
62+
var candidates = parsedJson["candidates"];
63+
if (candidates.HasValues)
64+
{
65+
var location = candidates[0]["location"];
66+
double x = (double)location["x"];
67+
double y = (double)location["y"];
68+
69+
return MapPointBuilder.CreateMapPoint(x, y, SpatialReferences.WGS84);
70+
}
71+
72+
MessageBox.Show("No matching address found.");
73+
return null;
74+
}
75+
}
76+
77+
private void AddPointToMap(MapPoint point)
78+
{
79+
// Create a simple marker symbol
80+
var color = ColorFactory.Instance.CreateRGBColor(255, 0, 0);
81+
var markerSymbol = SymbolFactory.Instance.ConstructPointSymbol(color, 10, SimpleMarkerStyle.Circle);
82+
83+
// Create a CIMPointGraphic
84+
var pointGraphic = new CIMPointGraphic
85+
{
86+
Location = point,
87+
Symbol = markerSymbol.MakeSymbolReference()
88+
};
89+
90+
// Add the graphic to the map as an overlay
91+
MapView.Active.AddOverlay(pointGraphic);
92+
}
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<ArcGIS defaultAssembly="geocode from a button.dll" defaultNamespace="geocode_from_a_button" xmlns="http://schemas.esri.com/DADF/Registry" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.esri.com/DADF/Registry file:///C:/Program%20Files/ArcGIS/Pro/bin/ArcGIS.Desktop.Framework.xsd">
2+
<AddInInfo id="{77983c93-cd08-4c39-9cd9-adad5adb38f8}" version="1.0" desktopVersion="3.3.52636" product="ArcGISPro">
3+
<Name>geocode_from_a_button</Name>
4+
<Description>geocode_from_a_button description</Description>
5+
<Image>Images\AddinDesktop32.png</Image>
6+
<Author>pau11750</Author>
7+
<Company>ESRI</Company>
8+
<Date>10/22/2024 1:32:00 PM</Date>
9+
<Subject>Framework</Subject>
10+
<!-- Note subject can be one or more of these topics:
11+
Content, Framework, Editing, Geodatabase, Geometry, Geoprocessing, Layouts, Map Authoring, Map Exploration -->
12+
</AddInInfo>
13+
<modules>
14+
<insertModule id="geocode_from_a_button_Module" className="Module1" autoLoad="false" caption="Module1">
15+
<!-- uncomment to have the control hosted on a separate tab-->
16+
<tabs>
17+
<!--<tab id="geocode_from_a_button_Tab1" caption="New Tab">
18+
<group refID="geocode_from_a_button_Group1"/>
19+
</tab>-->
20+
</tabs>
21+
<groups>
22+
<!-- comment this out if you have no controls on the Addin tab to avoid
23+
an empty group-->
24+
<group id="geocode_from_a_button_Group1" caption="test long name group" appearsOnAddInTab="true">
25+
<!-- host controls within groups -->
26+
<button refID="geocode_from_a_button_Button1" size="large" />
27+
<button refID="geocode_from_a_button_Button2" size="large" />
28+
</group>
29+
</groups>
30+
<controls>
31+
<!-- add your controls here -->
32+
<button id="geocode_from_a_button_Button1" caption="Button 1" className="Button1" loadOnClick="true" smallImage="GenericButtonBlue16" largeImage="GenericButtonBlue32">
33+
<tooltip heading="Tooltip Heading">Tooltip text<disabledText /></tooltip>
34+
</button>
35+
<button id="geocode_from_a_button_Button2" caption="Button 2" className="Button2" loadOnClick="true" smallImage="GenericButtonBlue16" largeImage="GenericButtonBlue32">
36+
<tooltip heading="Tooltip Heading">Tooltip text<disabledText /></tooltip>
37+
</button>
38+
</controls>
39+
</insertModule>
40+
</modules>
41+
</ArcGIS>
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using ArcGIS.Core.CIM;
2+
using ArcGIS.Core.Data;
3+
using ArcGIS.Core.Geometry;
4+
using ArcGIS.Desktop.Catalog;
5+
using ArcGIS.Desktop.Core;
6+
using ArcGIS.Desktop.Editing;
7+
using ArcGIS.Desktop.Extensions;
8+
using ArcGIS.Desktop.Framework;
9+
using ArcGIS.Desktop.Framework.Contracts;
10+
using ArcGIS.Desktop.Framework.Dialogs;
11+
using ArcGIS.Desktop.Framework.Threading.Tasks;
12+
using ArcGIS.Desktop.KnowledgeGraph;
13+
using ArcGIS.Desktop.Layouts;
14+
using ArcGIS.Desktop.Mapping;
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
using System.Windows.Input;
21+
22+
namespace geocode_from_a_button
23+
{
24+
internal class Module1 : Module
25+
{
26+
private static Module1 _this = null;
27+
28+
/// <summary>
29+
/// Retrieve the singleton instance to this module here
30+
/// </summary>
31+
public static Module1 Current => _this ??= (Module1)FrameworkApplication.FindModule("geocode_from_a_button_Module");
32+
33+
#region Overrides
34+
/// <summary>
35+
/// Called by Framework when ArcGIS Pro is closing
36+
/// </summary>
37+
/// <returns>False to prevent Pro from closing, otherwise True</returns>
38+
protected override bool CanUnload()
39+
{
40+
//TODO - add your business logic
41+
//return false to ~cancel~ Application close
42+
return true;
43+
}
44+
45+
#endregion Overrides
46+
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"geocode_from_a_button": {
4+
"commandName": "Executable",
5+
"executablePath": "C:\\Program Files\\ArcGIS\\Pro\\bin\\ArcGISPro.exe"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)