No Device? No GPS? No Matter! – (With Code!)

Necessity is the mother of all invention and so I went about creating a quick application to better help simulate GPS coordinates when developing for WP7. Most developers don’t have a test device and two common reasons for asking for one is for the GPS and for the accelerometer. The latter has been simulated quite well (via the HTC HD2 or the Wiimote), but the former is usually simulated by manually typing in (or downloading a set of) coordinates. I’m currently reading up on WCF and decided to kill two birds with one stone – learn WCF and help other developers.

So, what is it? Simple – A locally hosted server displaying Bing maps and a client (e.g. your WP7 app) consuming those coordinates. As you move your mouse along the map, the GPS coordinates are retrieved and sent to the phone. This opens up quite a few possibilities. For example, you could store the coordinates every X seconds  in a list to generate a ‘path’ that simulates someone driving from one point to another. If you need to know exact coordinates for your application, you can also extend the app so that you can  press a key whenever you want to store a particular coordinate. Whatever you use it for, hopefully it saves you time and makes it easier to develop the GPS aspects of your application. Check out the video and source code below.

The source code is not designed to be used in production and was created to easily simulate GPS location. Functionality over form in this case, so don’t take it as a best-practise guide ;).

Hope you find it useful! Any problems understanding the code, or if it’s not working for you, feel free to comment and I’ll try and help.

Update – You can also see an updated version by @preconsult over here as well as combining this sim with the Mock Location API.

Tom Verhoeff has pointed out that you may need to add Replace(‘.’,’,’) to the values for it to parse. This is down to your locale, so changing locale settings would also work.

Download Source Code

Retweet This Post


@keyboardP

Share

44 Responses to No Device? No GPS? No Matter! – (With Code!)

  1. Pingback: Tweets that mention No Device? No GPS? No Matter! – (With Code!) « Phone 7 -- Topsy.com

  2. Pingback: 给你的Windows Phone 7模拟器加入GPS和加速度传感器模拟功能 | WPMind - Windows Phone 资讯观点、应用软件、开发技术交流专业站!

  3. Prabhu says:

    Hey, this is cool! Will try the code out today….

  4. Pingback: Simulate GPS functionality in WP7 without device

  5. Pingback: Simulate GPS Functionality for WP7 App Development | Mobile | RizwanAshraf.com

  6. Pingback: GPS Unterstützung im WP7 Emulator simulieren | Smartphones mit Windows Phone 7

  7. Pingback: GPS Simulator for WP7 development | WindowsSmartphones.com

  8. jkoseattle says:

    When I open the app in VS, I get a lot of namespace errors. I’m running VS2010 Express for Windows Phone. Is that why? do I need the full version of VS to run this code? If that’s not why, then what do I need to do to make this work?

    • keyboardP says:

      Hi,
      Sorry, I deleted my previous comment as I’ve tried running both solutions in Visual Studio for Windows Phone and they both seem to work. Could you please let me know what the error messages are?
      Thanks

  9. jkoseattle says:

    Running GPSSim_Consumer I get a large number of exceptions. Here is the first one. They are all along these lines:
    Error 1 The type or namespace name ‘Navigation’ does not exist in the namespace ‘System.Windows’ (are you missing an assembly reference?) C:\Visual Studio 2010\Projects\GPS Sim – keyboardP\GPSSim Consumer\GPSSim Consumer\App.xaml.cs 11 22 GPSSim Consumer

    Running VEMap, I get this error at host.Open():
    System.ServiceModel.AddressAccessDeniedException was unhandled
    Message=HTTP could not register URL http://+:8000/GPSSim/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
    Source=System.ServiceModel
    StackTrace:
    at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
    at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
    at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
    at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.HttpChannelListener.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open()
    at GPSSim.Program.Main() in C:\Visual Studio 2010\Projects\GPS Sim – keyboardP\VEMap\VEMap\Program.cs:line 60
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException: System.Net.HttpListenerException
    Message=Access is denied
    Source=System
    ErrorCode=5
    NativeErrorCode=5
    StackTrace:
    at System.Net.HttpListener.AddAllPrefixes()
    at System.Net.HttpListener.Start()
    at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
    InnerException:

    • keyboardP says:

      Ah okay, I think I know what the problem might be for the second one. It might come down to this bit:
      Uri baseAddress = new Uri(“http://localhost:8000/GPSSim”);
      I think it might be down to you not having permission on your system to host a service at that location. I’m the administrator on my development machine, so I was able to create a namespace there.

      For the first error, I’m not quite sure why that’s happening. I’ll see if I can reproduce it (or if someone else can, please let me know). What you could try doing is removing all the phone reference assemblies and readding them again (main one is the Microsoft.Phone reference). This could be down to us having different versions of the DLL (I’m running the public July CTP).

      • jkoseattle says:

        I have added the permission to http://localhost:8000/GPSSim successfully using netsh, but the program does not display the webpage. I do get the frame on the right with the Latitude and Longitudetext boxes, but the main page where the map would be displayed does not resolve. If I simply enter that address directly into IE, it tells me I have created a service. Any ideas?

        As for the Consumer app, it was indeed due to older versions of the dlls on my machine, and is now resolved.

      • keyboardP says:

        Hi,
        I think this might be because of the directory being hard coded in the file (since it launches the HTML file that’s found in the solution). If you open up the code for the form and scroll down to Line 71 (or thereabouts), there should be a line that reads “webBrowser1.Navigate(new Uri(@”file:///C:\Users\K P\Documents\Visual Studio 2010\Projects\VEMap\VEMap\map.html”, UriKind.RelativeOrAbsolute));”.

        Try changing that to the directory where the html file is located on your computer. It might just be a case of changing the “K P” to your user name, if the rest of the directory structure is the same.

        Let me know if that helps 🙂

  10. jkoseattle says:

    As for the Consumer app, I removed all the Windows.Phone references you supplied and added in the ones I had, and that took care of most of the problems.

    In app.xaml.cs, I now get this error:
    Error 1 The type or namespace name ‘LaunchingEventArgs’ could not be found (are you missing a using directive or an assembly reference?) C:\Visual Studio 2010\Projects\GPS Sim – keyboardP\GPSSim Consumer\GPSSim Consumer\App.xaml.cs 40 59 GPSSim Consumer

    I get the same error for the three similar empty methods declared right after this one.

    Lastly, I get this:
    Error 5 No overload for ‘CompleteInitializePhoneApplication’ matches delegate ‘System.Windows.Navigation.NavigatedEventHandler’ C:\Visual Studio 2010\Projects\GPS Sim – keyboardP\GPSSim Consumer\GPSSim Consumer\App.xaml.cs 96 36 GPSSim Consumer

    I have not researched the problem on the other app, but I agree it’s probably something to do with not having permissions.

    • keyboardP says:

      Hi,
      I believe you might be using a different version of the DLL (possibly internal?) which is causing these problems. ‘LaunchingEventArgs’ (and possibly the others) are part of the Microsoft.Phone.Shell reference for me. Try adding ‘using Microsoft.Phone.Shell’ at the top and see if that fixes the problem. If not, then the DLL you replaced it with may not be functioning in the same way, or have the same namespaces. If you can see what namespace LaunchingEventArgs should be for your DLL, that might solve the problem.

  11. Pingback: GPS Simulator « Windows Phone 7 App Find of the Day

  12. Pingback: Mike Ormond's Blog

  13. Pingback: [被遗弃的小指]给你的Windows Phone 7模拟器加入GPS和加速度传感器模拟功能 | WP7爱好者- Windows Phone 7(www.iwp7.com)

  14. Please make your code available at GitHub. This is very easy and good way for other developers to get your source code changes ASAP and if some dev adds new features he can provide you with patch (you decide if you apply patch or not). If you have further questions or need help with GitHub then please feel free to contact me.

  15. Kim Bouwman says:

    Hi,
    Great work, very helpful!
    I’m currently working on a nonprofit that involves Windows Phone 7 GPS Development, and I was a bit disappointed that the emu didn’t supported GPS faking out of the box. Your code gave me an idea!

    Cheers!

    • keyboardP says:

      You’re welcome! 🙂 Glad you found it useful.

      • Kim Bouwman says:

        Hey,

        I have integrated your code with the code of Peter Torr, so the System.Device.Location can be replaced with GPSSim.Device.Location. The GeoCoordinateWatcher will get geocoordinates from your desktop service as drawn previously. Your desktop app has been extended to load and save geotracks. I also did something about that globalization stuff…

        May be you want to have it….
        Cheers

      • keyboardP says:

        Hi Kim,

        I believe Mike Ormond has done something similar. Is your project along the same lines, or is it slightly different?
        You’re more than welcome to fork from the Github project or, if you prefer, I can link to your project in this post.

        Thanks.

  16. Kim Bouwman says:

    Ha,
    I am a bit late I guess.. :-),
    I didn’t found his post while searching for the GPS stuff in google….
    Thanks for the link!

  17. jerry says:

    Hi ,

    I think your simulator is great , however I have the same problem as one of your other users. It gives me an error on the .host.Open(); statement and gives me this error

    HTTP could not register URL http://+:8000/GPSSim/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

    I have full administrator control and I have been careful to change line 71 of Form1.cs to the correct subdirectory.

    Do you have any ideas?

    Thanks Jerry

    • keyboardP says:

      Hi Jerry,
      I can think of two things to solve this. Since you’re running with full admin control, you should be able to right-click on the Visual Studio shortcut and click ‘Run as Administrator’. This should ensure that Visual Studio is also running with full control.
      If that doesn’t work, see if the suggestion here fixes it.
      Hope that helps!

  18. jerry says:

    Hi,

    I got it working. I has to right click on the icon for VS and the select Run as Adminstrator. Once I did this then the map and the simulator came up. Since it is about miller time here in Florida , I will try to hook up the phone to see the points tommorrow morning. Thanks a million for your help and application. This stuff is a lot of fun.

    Jerry

    • keyboardP says:

      Hi,
      Thanks a lot for confirming that the ‘run as admin’ method works, hopefully it should help anyone else who runs into the same problem. Glad you enjoyed the app. If you get an app into the Marketplace, feel free to let me know and I’ll create a post about it 🙂

  19. suryakant choudhary says:

    Your are amazing !!.. this is such a cool tool .. Thanks a millionnnnnn !!.. 😀

  20. Pingback: Windows Phone 7 Developer Device | Sean Briscoe

  21. fewde says:

    thanks

  22. bleveque says:

    Thanks for all ! I need to use a gps and your application is exactly what i want
    Just a problem, when I run the simulator, i have this problem :

    line 64, form1.cs (more exactly : tbThreshold.Text = CheckThreshold(oldLon, oldLat, double.Parse(lon), double.Parse(lat)).ToString();)

    Error : Le format de la chaîne d’entrée est incorrect. (The format of the input string is incorrect) (Sorry, i’m a french student, I hope my english is comprehensible)

    If i comment this line, the simulator work well (in appearance), but when i launch the GPSSimConsumer, i have the same problem with an other line (when i click on “Toggle reading”) :

    Line 132 (form1.cs of Simulator) : “data[0] = double.Parse(tbLatitude.Text);” ==> (The format of the input string is incorrect). I think it’s the same problem for the next line “data[1] = double.Parse(tbLongitude.Text);”.

    And obviously (or not), on the GPSConsumer, Reference.cs, line 247 (“System.Collections.ObjectModel.ObservableCollection _result = ((System.Collections.ObjectModel.ObservableCollection)(base.EndInvoke(“GetGPSData”, _args, result)));”), i have an exceptions problem (with the exception Exception :p).

    I hope that i have write enough detail and that my problem is obvious :p

    And again, thank for your application ! =)

    • keyboardP says:

      Hi bleveque

      Glad you like this application! II think the error is coming up because of the cultures being used. Try using these lines instead:

      tbThreshold.Text = CheckThreshold(oldLon, oldLat, double.Parse(lon), double.Parse(lat)).ToString(CultureInfo.InvariantCulture);
      data[0] = double.Parse(tbLatitude.Text, CultureInfo.InvariantCulture);
      data[1] = double.Parse(tbLongitude.Text, CultureInfo.InvariantCulture);

      Let me know if that fixes the problem 🙂

  23. bleveque says:

    Ho ! I’m so dumb, i’ve just forgot to use “Replace(‘.’,’,’)”, so my value can’t be parsed…
    So, it’s perfect, thank you again ! 😀

    (And with your solution, VS say that the cultureInfo object is unknow (but only for the Form1.cs’s Simulator’s))

    Thank, you save my life :p

  24. bleveque says:

    Hey ! Just a last question ^^’ (maybe obvious)
    I’ve implemented an application around your application (from the data sent by the simulator and received by the method UpdatePosition() of @preconsult).

    Now if I want to program my device, do your application can run “normally” and received data from a real GPS or do i need to program a real reception ?

    (i’m so sorry, but i think that my question is not well-construct, i need to work my english^^’ I hope it’s comprehensible)

    • keyboardP says:

      Hi,

      The GPS simulator is just for development purposes. When you use it on a real device, you’ll need to program the receptor yourself. It’s quite straight forward and there’s an examples of how to do it here.

  25. Pingback: 超快递 beta版 » Windows Phone 7 资源汇总(超全)

Leave a reply to fewde Cancel reply