Mappoint + smartphone + C # development example

Source: Internet
Author: User
Mappoint + smartphone + C # development example

Original address:

Mappoint, smartphone and C #-Part 1


Mappoint, smartphone and C #-Part2

 

This article provides some simple applications that demonstrate how C # uses the mappoint SDK and how to display maps on Smartphone.

  Introduction

In the industry's rapid migration to the mobile field to obtain relevant profits, location-based services and applications are clearly one step ahead. At the front-end of technology, Microsoft has established a solid position in all these related technical fields.

 
The new mappoint SDK 3.5 provides an XML
Web services enable us to establish location-based services. This SDK helps us to obtain high-quality maps. The map also identifies specific locations (such as pizza stores and hospitals) and provides
The road map to the destination location. You can download this SDK

.

In terms of mobile operating systems, smartphone and PPC have built a strong position for Microsoft.
 
In this article, we will use C # To build some simple applications based on the mappoint SDK. We will first display a map of a specified location on Microsoft smartphone.

Let's first introduce mappoint. Mappoint exposes four web services:

1. Search for services-help us locate addresses, retrieve longitude and latitude, and geographical entities.

2. Display service-This service allows us to display the map of the specified address and set the size and view of the displayed map. At the same time, we can also set a "Graph nail" to serve as a visual tag of the reader.

3. Route service-This service allows us to generate routes, calculate the distance between two locations, and provide driving directions.

4. General Service-it is a tool that is the public part of the preceding three web services. Provides some services, such as country information and map data source information.

To use the mappoint web service, you must obtain a developer account. Click this link

To register. You can register and receive an evaluation account. If you are an msdn user, you can receive a one-year free subscription.

Now let's analyze the code.

I will break down the application code. However, I strongly recommend that you read the basic knowledge of the mappoint SDK. The same content is repeated and does not add any value.

  Display the specified Map

Download
And open the project solution. Open mappointwrapper. CS and replace _ mappointusername and _ mappointpassword with your own mappoint developer username and password.

Form1.cs contains a menu object that obtains detailed information about the address of the displayed map.

Click the "Get Map" menu to create an address object, which has been set to "mappoint. Na ". Below are some available data sources.

1. mappoint. EU-Europe

2. mappoint. Na-North America

3. mappoint. Br-Brazil

4. mappoint. World-world

5. mappoint. Moon-monthly pulse chart

The following code uses the findservicesoap web service to retrieve location information based on the specified address. The Web Service must pass the mappoint developer account authentication. The Data Source Name and address must be provided.

Public static void getaddress (Address, string datasourcename,
Out indresults location, out viewbyheightwidth [] views)
{
Try
{
Findservicesoap locationservice = new findservicesoap ();
Locationservice. Credentials = new system. net. networkcredential (_ mappointusername, _ mappointpassword );

Locationservice. preauthenticate = true;

Findaddressspecification locationdata = new findaddressspecification ();
Locationdata. Your CENAME = Your CENAME;
Locationdata. inputaddress = address;

Location = locationservice. findaddress (locationdata );

Views = new viewbyheightwidth [1];
Views [0] = location. Results [0]. foundlocation. bestmapview. byheightwidth;
}
Catch (exception ex)
{
Throw new exception (ex. Message, ex );
}
}

 
After obtaining detailed information about the location, the data is further sent to the getmap method. This method uses the "renderservicesoap" Web service. This service also
Authentication information is required. Mappoint provides a "Graph nail", a visual marker used to identify addresses on a map. You can select one from the default icon group and set an appropriate name. This
In addition, it creates a mapspecification object, which maintains the view, MAP, image format, and other content. We call the getmap method of renderservice.
It is retrieved as a stream and displayed as a bitmap.

Public static bitmap getmap (findresults location, viewbyheightwidth [] views, string inclucename,
Point mapdimensions)
{
Try
{
Renderservicesoap renderservice = new renderservicesoap ();
Pushpin [] pushpins = new pushpin [1];
Mapspecification mapspec = new mapspecification ();
Renderservice. Credentials = new system. net. networkcredential (_ mappointusername, _ mappointpassword );
Renderservice. preauthenticate = true;

Pushpins [0] = new pushpin ();
Pushpins [0]. icondatasource = "mappoint. icons ";
Pushpins [0]. iconname = "0 ";
Pushpins [0]. Label = location. Results [0]. foundlocation. entity. Name;
Pushpins [0]. latlong = views [0]. centerpoint;
Pushpins [0]. returnshotarea = true;

Mapspec. datasourcename = datasourcename;
Mapspec. Views = views;
Mapspec. pushpins = pushpins;
Mapspec. Options = new mapoptions ();
Mapspec. Options. format = new imageformat ();
Mapspec. Options. format. width = mapdimensions. X;
Mapspec. Options. format. Height = mapdimensions. Y;
Mapimage [] mapimages = renderservice. getmap (mapspec );

System. Io. Stream streamimage = new system. Io. memorystream (mapimages [0]. mimedata. bits );
Bitmap bitmap = new Bitmap (streamimage );
Return bitmap;
}
Catch (exception ex)
{
Throw new exception (ex. Message, ex );
}
}

We have completed the transaction. The following figure shows the displayed map. Pay attention to the correct location identified by the map's "dingtalk.

Calculate route and distance

Next we will look for the route between the two addresses and calculate the distance between them.

Download

And open the project solution. Open mappointwrapper. CS and replace _ mappointusername and _ mappointpassword with your own mappoint username and password.

Form1.cs contains a menu object that obtains the address details of the displayed map.

When you click "Get route", the address object is created and the data source is set to "mappoint. Na ". Now, to find the route between two locations, we need to execute the following transactions:

1. Identify the latitude and longitude of the address.

2. Obtain a map of the routes identified by the pushpins.

The following code uses the findservicesoap web service to obtain the latitude and longitude of the address. The findresults class has the attribute "latlong", which gives the latitude and longitude of the given address.

Public static latlong getaddress (Address, string datasourcename,
Out findresults location, out viewbyheightwidth [] views)
{
Try
{
Findservicesoap locationservice = new findservicesoap ();
Locationservice. Credentials = new system. net. networkcredential (_ mappointusername, _ mappointpassword );
Locationservice. preauthenticate = true;
Findaddressspecification locationdata = new findaddressspecification ();
Locationdata. Your CENAME = Your CENAME;
Locationdata. inputaddress = address;

Location = locationservice. findaddress (locationdata );
Views = new viewbyheightwidth [1];
Views [0] = location. Results [0]. foundlocation. bestmapview. byheightwidth;
Return location. Results [0]. foundlocation. latlong;
}
Catch (exception ex)
{
Throw new exception (ex. Message, ex );
}
}

The obtained latitude and longitude are passed to the following method to obtain the map:

Public static double getmapforroute (Out bitmap [] routemaps,
Out viewbyheightwidth [] views, latlong [] latitudelong.pdf,
String datasourcename, point mapdimension)
{
Routeservicesoap routeservice = new routeservicesoap ();
Routeservice. Credentials = new system. net. networkcredential (_ mappointusername, _ mappointpassword );
Routeservice. preauthenticate = true;

Userinforouteheader routeuserinfo = new userinforouteheader ();
Routeuserinfo. defaultdistanceunit = distanceunit. kilometer;
Routeservice. userinforouteheadervalue = routeuserinfo;

Mapoptions = new mapoptions ();
Mapoptions. format = new imageformat ();
Mapoptions. format. width = mapdimension. X;
Mapoptions. format. Height = mapdimension. Y;

Route route;
Route = routeservice. calculatesimpleroute (latitudelongstrap, CENAME, segmentpreference. Quickest );
Int mapdirelength length = route. itinerary. segments [0]. Directions. Length + 1;
Views = new viewbyheightwidth [mapdirelength length];
Routemaps = new bitmap [mapdirelength length];

Pushpin [] pushpins = new pushpin [mapdirelength length];

For (INT idx = 0; idx <= MapDirectionLength-1; idx ++)
{
Pushpins [idx] = new pushpin ();
Pushpins [idx]. icondatasource = "mappoint. icons ";
If (idx! = MapDirectionLength-1)
{
Views [idx] = route. itinerary. segments [0]. Directions [idx]. View. byheightwidth;
Pushpins [idx]. iconname = "0 ";
Pushpins [idx]. latlong = route. itinerary. segments [0]. Directions [idx]. latlong;
}
Else
{
Views [idx] = route. itinerary. segments [1]. Directions [0]. View. byheightwidth;
Pushpins [idx]. iconname = "1 ";
Pushpins [idx]. latlong =
Route. itinerary. segments [1]. Directions [0]. latlong;
}
Pushpins [idx]. returnshotarea = true;
}

Mapspecification mapspec = new mapspecification ();
Mapspec. datasourcename = datasourcename;
Mapspec. Options = mapoptions;
Mapspec. Views = views;
Mapspec. pushpins = pushpins;
Mapspec. Route = route;
Mapimage [] mapimages;

Renderservicesoap renderservice = new renderservicesoap ();
Renderservice. Credentials = new system. net. networkcredential (_ mappointusername, _ mappointpassword );
Renderservice. preauthenticate = true;
Mapimages = renderservice. getmap (mapspec );

For (INT idx = 0; idx <mapdirelength length; idx ++)
{
Routemaps [idx] = new Bitmap (new system. Io. memorystream (mapimages [idx]. mimedata. bits ));
}
Return route. itinerary. segments [0]. distance;
}

We use the "routeservicesoap" Web service to generate routemap. After authentication, the header value is set to distanceunit. The Web Service provides a "calculatesimpleroute" method, which calculates the outbound line based on the corresponding latitude and longitude array.

It returns a route object. At the same time, we also set up "map nails" for the route ". It calls the renderservicesoap web service again, but the output content is different this time. We will get a map sequence, from the beginning to the end of the map.

The following is a screenshot of the route.

 
It is collected on the bitmap array and properly displayed on the picturebox. "Route. itinerary. segments [0]. distance;" returned
The distance between two addresses. This distance can be measured in miles or kilometers. It is set in the header value of the web service of routeservicesoap.

The following is a screenshot showing the distance between two addresses.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.