Click here to Skip to main content
15,896,557 members
Articles / Mobile Apps / Windows Mobile

Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant

Rate me:
Please Sign up or sign in to vote.
4.94/5 (126 votes)
10 Feb 2009CPOL10 min read 775.8K   11.1K   381  
A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Collections.Specialized;
using System.Xml;
using DeepCast;
using System.IO;
using DeepCast.Location;

namespace DeepCast.Services
{
    internal delegate void MapImageChangedDelegate(string mapUrl);

    /// <summary>
    /// Gets a Yahoo map image based on the current lat/long.
    /// </summary>
    internal class LocationImageService : GeoService
    {
        internal event MapImageChangedDelegate MapChanged;
        private GeoLocation _currentLocation;

        private const string Yahoo_Maps_Uri = "http://local.yahooapis.com/MapsService/V1/mapImage?";

        /// <summary>
        /// Updates the image.
        /// </summary>
        internal void Update()
        {
            if (_currentLocation != null)
            {
                Update(_currentLocation);
            }
        }
        /// <summary>
        /// Updates the image.
        /// </summary>
        /// <param name="location">The location.</param>
        internal override void Update(GeoLocation location)
        {
            if(Zoom == 0)
            { 
                // Default to a zoom level of 6 (valid values are 1 - 12).
                Zoom = 6;
            }

            _currentLocation = location;

            // Build a map api query
            Dictionary<string, string> parameters = new Dictionary<string, string>() 
            { 
                {"appid", Settings.YahooMapsApiKey},
                {"latitude", location.Latitude.ToStringNumeric()},
                {"longitude", location.Longitude.ToStringNumeric()},
                {"image_type", "gif"},
                {"image_height", "195"},
                {"image_width", "195"},
                {"zoom", Zoom.ToString()}
            };

            XDocument doc = XDocument.Load(Yahoo_Maps_Uri + Utility.BuildQueryString(parameters));

            // Get the image text from the xml
            foreach (var element in doc.Descendants())
            {
                if (!string.IsNullOrEmpty(element.Value))
                {
                    if (MapChanged != null)
                    {
                        MapChanged(element.Value);
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Gets the service's friendly name.
        /// </summary>
        /// <value>The name.</value>
        internal override string Name
        {
            get { return "Maps"; }
        }
        /// <summary>
        /// Gets or sets the zoom level.
        /// </summary>
        /// <value>The zoom.</value>
        internal int Zoom { get; set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!

http://www.pagelabs.com

Comments and Discussions