Introduction
This sample demonstrates rendering a map, adding a new Pushpin at each point.the user clicks, and enabling the user to reposition Pushpins on the map by clicking and dragging. It also displays the address, latitude, and longitude values for each pushpin, and lets users perform simple searches for places.
Note:
Before you run this sample,you must apply a map point evaluation account on MS site.
https://mappoint-css.live.com/MwsSignUp/eval.aspx and then open app.config in your project and type your name and password in <add key="MPUser" value="" /> <add key="MPPass" value="" />.
Using the code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Net;
using System.Resources;
using System.Windows.Forms;
using System.Data;
using System.Security.Permissions;
using DragPushpinOnMap.MapPointService;
[assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution = true)]
namespace DragPushpinOnMap
///<summary>
/// This sample demonstrates rendering a map, adding a new Pushpin at each point
/// the user clicks, and enabling the user to reposition Pushpins on the map by
/// clicking and dragging. It also displays the address, latitude, and longitude
/// values for each pushpin, and lets users perform simple searches for places.
///</summary>
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components = null;
// MapPoint services to be used.
private FindServiceSoap findService;
private RenderServiceSoap renderService;
// Set up map info, and define a new class for the PushpinPanel.
private ArrayList pushPins;
private MapSpecification currentMapSpecification;
private MapView currentView;
private PushpinPanel selectedPin;
public PushpinPanel SelectedPin
{
get
{
return selectedPin;
}
set
{
if(selectedPin != null)
selectedPin.Selected = false;
if (value == null)
{
throw new ArgumentNullException("value");
}
selectedPin = value;
selectedPin.Selected = true;
propertyGrid1.SelectedObject = value.Info;
}
}
private System.Windows.Forms.PictureBox mapPictureBox;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.PropertyGrid propertyGrid1;
public Form1()
{
// Required for Windows Form Designer support.
InitializeComponent();
pushPins = new ArrayList();
// Create services.
try
{
// Set up the service variables, get application
// settings from the config file.
string myUserID = ConfigurationManager.AppSettings.Get("MPUser");
string myPassword = ConfigurationManager.AppSettings.Get("MPPass");
NetworkCredential myCredentials = new NetworkCredential(myUserID, myPassword);
renderService = new RenderServiceSoap();
renderService.Credentials = myCredentials;
findService = new FindServiceSoap();
findService.Credentials = myCredentials;
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
// Set up event handlers.
button1.Click += new EventHandler(button1_Click);
mapPictureBox.MouseDown +=
new System.Windows.Forms.MouseEventHandler(map_MouseDown);
// Set the cursor for the picture box.
mapPictureBox.Cursor =
new Cursor(GetType().Assembly.GetManifestResourceStream(
"DragPushpinOnMap.Cursor1.cur"));
// Make the find button the default action button.
AcceptButton = button1;
// Hide help.
propertyGrid1.HelpVisible = false;
// Initialize the map.
SetMapView("Seattle");
}
//
// Event Handlers
//
// Button event handler for finding addresses.
private void button1_Click(Object sender, EventArgs e)
{
SetMapView(textBox1.Text);
propertyGrid1.SelectedObject = null;
}
// MouseDown event handler for the picture box.
private void map_MouseDown(Object sender,
System.Windows.Forms.MouseEventArgs e)
{
// Create the pin where the user clicked.
PushpinPanel pin = new PushpinPanel(this);
pin.Location = new System.Drawing.Point(e.X, e.Y);
pin.Size = new System.Drawing.Size(16, 16);
if(pin.UpdateLocation() == false)
{
MessageBox.Show("Invalid location.");
pin.Dispose();
pin = null;
}
else
{
// Add pin to maps array of pins.
pushPins.Add(pin);
// Add to picture box and display.
mapPictureBox.Controls.Add(pin);
pin.Show();
// Refresh display.
Refresh();
// Set it as the selected pin.
SelectedPin = pin;
}
}
//
// Methods
//
// Find the specified address, display the map if found.
private void SetMapView(string address)
{
FindSpecification findSpec = new FindSpecification();
FindResults findResults = null;
findSpec.DataSourceName = "MapPoint.NA";
findSpec.InputPlace = address;
try
{
findResults = findService.Find(findSpec);
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
// Inform the user if no results were found (object was null).
if(findResults == null)
{
MessageBox.Show("No results were found.");
}
else
{
// Inform the user if no results were found, and then return.
if(findResults.NumberFound == 0)
{
MessageBox.Show("No results were found.");
return;
}
// Map was found, clean up and display it.
// Reset Pushpins.
for(int i = 0; i < pushPins.Count; i++)
{
mapPictureBox.Controls.Remove((PushpinPanel)pushPins
);
}
pushPins = new ArrayList();
// Set the best map view for the top search result.
currentView =
findResults.Results[0].FoundLocation.BestMapView.ByHeightWidth;
// Get the map for the best view.
currentMapSpecification =
new MapSpecification();
// Specify the image size of the map.
currentMapSpecification.Options = new MapOptions();
currentMapSpecification.Options.Format = new ImageFormat();
currentMapSpecification.Options.Format.Height = mapPictureBox.Height;
currentMapSpecification.Options.Format.Width = mapPictureBox.Width;
// Set the map view to the previous one that was found.
currentMapSpecification.Views = new MapView[1];
currentMapSpecification.Views[0] = currentView;
currentMapSpecification.DataSourceName = "MapPoint.NA";
try
{
// Get the map image.
MapImage tempImage = renderService.GetMap(currentMapSpecification)[0];
mapPictureBox.Image =
new Bitmap(new System.IO.MemoryStream(tempImage.MimeData.Bits, false), true);
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
// Get the LatLong position for a given pixel on the map.
public LatLong ConvertToLatLong(PixelCoord coordinate)
{
// ConvertToLatLong requires an array of PixelCoord objects.
PixelCoord [] coordinates = {coordinate};
// ConvertToLatLong returns an array of LatLong objects.
LatLong [] locations = null;
LatLong returnLatLong = null;
locations = renderService.ConvertToLatLong(coordinates,
currentMapSpecification.Views[0],
currentMapSpecification.Options.Format.Width,
currentMapSpecification.Options.Format.Height);
if(locations != null && locations.Length > 0)
{
returnLatLong = locations[0];
}
return returnLatLong;
}
public DragPushpinOnMap.MapPointService.Location GetLocationInfo(
LatLong latLong)
{
// GetLocationInfo returns an array of Location objects.
DragPushpinOnMap.MapPointService.Location [] locations = null;
DragPushpinOnMap.MapPointService.Location returnLocation = null;
locations = findService.GetLocationInfo(latLong, "MapPoint.NA", null);
if(locations != null && locations.Length > 0)
{
returnLocation = locations[0];
}
return returnLocation;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
if(renderService != null)
{
renderService.Dispose();
}
if(findService != null)
{
findService.Dispose();
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.mapPictureBox = new System.Windows.Forms.PictureBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.mapPictureBox)).BeginInit();
this.SuspendLayout();
//
// mapPictureBox
//
this.mapPictureBox.BackColor = System.Drawing.SystemColors.Control;
this.mapPictureBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
resources.ApplyResources(this.mapPictureBox, "mapPictureBox");
this.mapPictureBox.Name = "mapPictureBox";
this.mapPictureBox.TabStop = false;
//
// textBox1
//
resources.ApplyResources(this.textBox1, "textBox1");
this.textBox1.Name = "textBox1";
//
// label1
//
resources.ApplyResources(this.label1, "label1");
this.label1.Name = "label1";
//
// button1
//
resources.ApplyResources(this.button1, "button1");
this.button1.Name = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// propertyGrid1
//
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
resources.ApplyResources(this.propertyGrid1, "propertyGrid1");
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.ToolbarVisible = false;
//
// label2
//
resources.ApplyResources(this.label2, "label2");
this.label2.Name = "label2";
//
// label3
//
resources.ApplyResources(this.label3, "label3");
this.label3.Name = "label3";
//
// Form1
//
resources.ApplyResources(this, "$this");
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.propertyGrid1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.mapPictureBox);
this.Name = "Form1";
((System.ComponentModel.ISupportInitialize)(this.mapPictureBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click_1(object sender, EventArgs e)
{
}
}
}