EziData Solutions

Web, NET and SQL Server

Sending an SMS from Window Phone 7 App

In a previous post I covered how to send emails to one of your contacts directly from a Windows Phone 7 application. WP7 enables you to support a number of other communication channels, including SMS, right inside your application.

private void Button_Click(object sender, RoutedEventArgs e)

{

    SmsComposeTask sms = new SmsComposeTask();

    sms.To = "555 1234";

    sms.Body = "Wow sending an sms from WP7 is so easy";

    sms.Show();

}

 

While it is possible to send an SMS message directly to a mobile number (as shown in the code above), it would normally be preferable to allow the user to select one of their contacts before sending the message.

private void Button_Click(object sender, RoutedEventArgs e)

{

    PhoneNumberChooserTask phone = new PhoneNumberChooserTask();

    phone.Completed += new EventHandler<PhoneNumberResult>(phone_Completed);

    phone.Show();

}

 

void phone_Completed(object sender, PhoneNumberResult e)

{

    //only continue if the user selected a contact

    if (e.TaskResult == TaskResult.OK)

    {

        SmsComposeTask sms = new SmsComposeTask();

        sms.To = e.PhoneNumber;

        sms.Body = "Wow sending an sms from WP7 is so easy";

        sms.Show();

    }

}

 

The SMSComposeTask launches the messaging application on the OS and displays the new message. As with all the other Launcher tasks we have investigated, the user has the option to edit the message or even cancel sending it altogether.

Posted: Apr 15 2011, 05:57 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Windows Phone 7 | WP7

Using the Bing Maps API

As part of developing a community directory website I needed to migrate a large number of Name/Address records from several existing systems. These records contained the usual street and suburb fields, but lacked Latitude and Longitude values.

Instead of doing the geocoding when the record is displayed in the map, I decided to utilise the Bing Maps API to perform the geooding of these addresses and store the resulting Latitude and Longitude in the database.

To perform the geocoding I used the Geocode Service, which is part of Bing Maps SOAP Services. According to their website

Microsoft® Bing Maps SOAP Services is a set of programmable SOAP services that allow you to match addresses to the map, search for points of interest, integrate maps and imagery, return driving directions, and incorporate other location intelligence into your Web application.

The first step is to sign up for your Bing Maps API key – it's free and painless, especially if you have a Windows Live account already.

Our Address Class

As this project was dealing with the names and addresses of organisations, I created a simple class to store the Organisation details and another to store the Addresses where the organisation operated. The important parts of the OrganisationAddress class are shown below.

public class OrganisationAddress

{

    public string StreetAddress { get; set; }

    public double Latitude { get; set; }

    public double Longitude { get; set; }

}

As each new OrganisationAddress was created, I called a function that passed the address to the Geocode Service and returned the Lat and Long, which I then saved with the OrganisationAddress.

 

To get started, add a Service Reference named MyBingMap to your Visual Studio project. The Uri to the GeocodeService is http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl

Once you have the service reference, you can create a function that send the street address (consisting of the house number, street name and suburb) and as a GeocodeRequest object to the Bing Maps API. The snipet below shows part of the function that sends in our OrganisationAddress object that contains that StreetAddress property.

    //instantiate a GeocodeRequest and pass in your Bing Maps API key

    MyBingMap.GeocodeRequest map = new MyBingMap.GeocodeRequest();

    map.Credentials = new MyBingMap.Credentials();

    map.Credentials.ApplicationId = "Your API Key";

 

    //add the address components to the GeocodeRequest.Query

    map.Query = OrganisationAddress.StreetAddress;

 

    //the Confidence level controls how accurate you want the results

    //if your address is good then Confidence.High should return the location

    //but be warned, if the location can not be found you'll get lat,long of 0,0

    MyBingMap.ConfidenceFilter[] filters = new MyBingMap.ConfidenceFilter[1];

    filters[0] = new MyBingMap.ConfidenceFilter();

    filters[0].MinimumConfidence = MyBingMap.Confidence.High;

 

    //add the assigned ConfidenceFilter to the Options

    MyBingMap.GeocodeOptions options = new MyBingMap.GeocodeOptions();

    options.Filters = filters;

    map.Options = options;

 

    //instantiate a GeocodeResponse for the service passing in your GeocodeRequest

    MyBingMap.GeocodeServiceClient geoClient = new MyBingMap.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

    MyBingMap.GeocodeResponse geocodeResponse = geoClient.Geocode(map);

 

    //if we get a response back, then save the Lat/Long with our OrganisationAddress

    if (geocodeResponse.Results.Length > 0)

    {

        if (geocodeResponse.Results[0].Locations.Length > 0)

        {

            OrganisationAddress.Latitude = geocodeResponse.Results[0].Locations[0].Latitude;

            OrganisationAddress.Longitude = geocodeResponse.Results[0].Locations[0].Longitude;

        }

    }

If like me you get an error "The remote server returned an unexpected response: (407) Proxy Authentication Required" chances are it's because you are using a proxy server to access the web. In this case you'll need to add a few extra lines of code at the start of the previous code block to setup the credentials of the DefaultWebProxy.

    //if you're accessing the service via a proxy

    //you may need to specify your credentials

    IWebProxy wproxy = WebRequest.DefaultWebProxy;

 

    wproxy.Credentials = CredentialCache.DefaultNetworkCredentials;

    WebRequest.DefaultWebProxy = wproxy;

Posted: Apr 13 2011, 05:16 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.NET