Delphi XE5 iOS and Android One Line of Code App - Map My Location

Posted by on in Blogs
Michael Swindell challenged us to create mobile apps using Delphi XE5 for iOS and Android with only one line of code. After thinking about this for awhile, here is my first one line of code app, "Map My Location".  This app uses the TLocationSensor, TToolBar, TLabel and TWebBrowser components to create an Android and iOS app that displays a Google map of my current location.

To start this Delphi XE5 mobile project, choose the "File | New | FireMonkey Mobile Application - Delphi" IDE menu choice..  This brings up the Mobile Project template library.



I chose the "Blank Application" template. On the blank form I added a TToolBar component and set its alignment to Top.  I added a TLabel component to the TToolBar and set the label's Text property to "Map My Location".  I set the Label's TextAlign property to center and its Alignment proprty to alHorizCenter so that the label is in the center of the ToolBar.  I added a TWebBrowser component onto the form and set its Alignment property to alClient so that it takes over the rest of the form's client area.  I finally added a TLocationSensor component to the form so that I can get the latitude and longitude from the GPS sensor on my Apple iPhone and Samsung Galaxy S4.  I set the TLocationSensor's Active property to True so that it gets the location whenever I run the app.

Here is what the Delphi XE5 IDE looks after all of the components and property settings are made. You can click on each of the bitmaps to see larger versions of the form, object inspector and project manager.





For my one line of code app, I set the TLocationSensor's OnLocationChanged event handler by selecting the TLocationSensor, switched the Object Inspector to the Events tab and double clicked on the OnLocationChanged event to create the following event handler and added my "one line of code":

    procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
    const OldLocation, NewLocation: TLocationCoord2D);
    begin
    WebBrowser1.Navigate(
    Format(
    'https://maps.google.com/maps?q=%s,%s&output=embed',
    [NewLocation.Latitude.ToString, NewLocation.Longitude.ToString]
    )
    );
    end;


The line of code takes the NewLocation Latitude and Longitude that comes from the GPS, converts the double precision floating point values (TLocationCoord2D) to strings using the ToString helper function, inserts the two values into the string using the Format function and passes the resulting string to the TWebBrowser's Navigate method.



Then I compiled and deployed the app to my Android and iOS devices by activating each of the target platform choices.  The resulting screen grabs on my Apple iPhone 4S and Samsung Galaxy S4 show the one line code app running on each device.



What iOS and Android Delphi XE5 app can you create using one line of code? Join the fun of creating multi-device apps using Delphi and the FM Application Platform. If you don't have Delphi XE5, you can download the Instant Trial or Standard Trial at http://www.embarcadero.com/products/delphi/downloads.


About
Gold User, Rank: 1, Points: 2466
David Intersimone (known to many as David I.) is a passionate and innovative software industry veteran-often referred to as a developer icon-who extols and educates the world on Embarcadero developer tools. He shares his visions as an active member of the industry speaking circuit and is tapped as an expert source by the media. He is a long-standing champion of architects, developers and database professionals and works to ensure that their needs are folded into Embarcadero's strategic product plans. David holds a bachelor's degree in computer science from California Polytechnic State University at San Luis Obispo, California.

Comments

  • Guest
    Joseph G. Mitzen Sunday, 3 November 2013

    Either my math is really bad, or that's a lot more than one line of code!

    >What iOS and Android Delphi XE5 app can you create using one line of code?

    None, because Delphi has a lot of boilerplate code, no type inference, etc. Once I type
    Program Whatever;

    I've already used up my one line. :-( Same for declaring my first variable or writing a function header.

  • Guest
    davidi Sunday, 3 November 2013

    Joseph: the one line of actual written (by me) code:
    WebBrowser1.Navigate(
    Format(
    'https://maps.google.com/maps?q=%s,%s&output=embed',
    [NewLocation.Latitude.ToString, NewLocation.Longitude.ToString]
    )
    );

  • Guest
    davidi Sunday, 3 November 2013

    Joseph and others - I could write a blog post showing how you can build a multi-device, multi-tier, master detail database application with no lines of code using database components, datasnap :)

  • Guest
    Jenny Sunday, 3 November 2013

    Really?

    How useful is this kind of application and as developer how much money do you think I'm going to make selling this? Probably ZERO.

    Bring something useful, build useful demos like Apple and Google demos.

    This kind of demo doesn't help to convince my self to use Delphi for mobile development.

  • Guest
    davidi Sunday, 3 November 2013

    Jenny - thanks for the suggestion. This specific blog post was to show how to do something simple. We build many other much more involved demos for webinars, tours, articles and other blog posts.

    DO you have specific "useful demos like Apple and Google demos" in mind? Maybe you can post some URLs to demo scripts, videos and examples you like from Apple and Google and I can build the same ones using Delphi for iOS and Android - that would be fun.

  • Guest
    Joseph G. Mitzen Sunday, 3 November 2013

    Mr. Intersimone,

    >Joseph: the one line of actual written (by me) code:

    Fair enough, although the system does generate a reasonable amount of code.

    Would other development tools (such as the official ones) require writing any more code to produce the same thing? That would be an even more interesting example for me. Honestly, it's the Google Maps website that's really doing most of the work here, so I'd be surprised if it was much more difficult to write in another language (but I could be wrong).

    >Joseph and others - I could write a blog post showing how you can build a multi-device,
    >multi-tier, master detail database application with no lines of code using database components,
    >datasnap

    And I could write a real one-line program in Python ;-)

    sorted(set(open('somewords.txt')))

    That would open a file (a list of words), build a set of unique words, then return a list with the words sorted. There's nothing wrong with playing Code Golf with Delphi, but it would be good if we saw something really unique or powerful rather than just displaying a web page with the IDE generating the rest of the boilerplate code needed. Java is a verbose language; I'd love to see how long the Java equivalent is.

  • Guest
    Rolf Warnecke Sunday, 3 November 2013

    @davidi:

    A useful demo for me is it to show how can I perform a lot of datasets into a listbox. I write on a ios app who had 10 or more datasets. If I show it in a listbox the application hangs for a few second. Under the vcl the program doesn't hang if I like to show 10 or more datasets in a listbox.

  • Guest
    Dennis Sunday, 3 November 2013

    You should challenge Mr. Swindell to not use marketing stuff that won't work for developers ("One line of code only"). That's nothing they're interested in. They are interested in functional demos showing all the good things hidden in fmx - no matter how many lines of code they would consist of. Stop marketing for idiots, start marketing for developers.

  • Guest
    Bunny Sunday, 3 November 2013

    Oh! Such an amazing demo. Thank you very much for sharing expertise and deep insight with us.

    The spirit of the web now on mobile devices too. Does the demo work with other URLs too? If yes, Delphi is very flexible. We want more.

  • Guest
    John Sunday, 3 November 2013

    David, you asked for some demo video. I saw an interesing video showcasing an app that was made with Delphi XE5
    http://www.youtube.com/watch?v=kZkGBwHN1EQ

    This is the kind of things you should be adverting, showing how powerfull Delphi XE5 can be and what kind of applications could be made out of it... Also, this video shows how good the performance can be with Firemonkey, since they managed to make a very smooth application with Firemonkey (Something that people always bitch about, performance)

  • Guest
    Leonardo Herrera Sunday, 3 November 2013

    @Jenny - are you really asking for a revenue-generating app written in one line of code? Really?

    This is a nice little demo. I prefer this far more than creating a "hello world" app with Java: http://developer.android.com/training/basics/firstapp/index.html

  • Guest
    Paul Breneman Monday, 4 November 2013

    I'd like to make (and release) a port of the communication program here:
    www.CtrlTerm.com

    Just a socket version (no serial) would be very useful! Can anyone help me?

  • Guest
    David Intersimone Monday, 4 November 2013

    Bunny - don't try to read too much into this one line of code blog post - what I am highlighting is how to use two of the many pre-built components, TLocationSensor and TWebBrowser, in a simple app that compiles and runs on iOS and Android. Of course, we can build much more complex applications using these and other components and RTL functions.

  • Guest
    David Intersimone Monday, 4 November 2013

    John - thanks for the YouTube link - we do have a load of videos showing much more you can do with Delphi and mobile including all of the CodeRage Mobile and CodeRage 8 sessions. I am working on additional videos and blog posts based on developer suggestions including BlueTooth, Serial, and NFC communications just to name three. Keep the suggestions coming and I'll write and record more for all of you.

  • Guest
    David Intersimone Monday, 4 November 2013

    Dennis - how harsh can you get? We really appreciate all of our developer customers. I hope you didn't mean to take this simple component based demo and apply it to all of the work our developers, evangelists, MVPs and partners do to help teach developers all that you can do with Delphi.

  • Guest
    David Intersimone Monday, 4 November 2013

    Joseph - "There’s nothing wrong with playing Code Golf with Delphi, but it would be good if we saw something really unique or powerful rather than just displaying a web page with the IDE generating the rest of the boilerplate code needed. Java is a verbose language; I’d love to see how long the Java equivalent is."

    I know that you can simply use your Google or Apple Maps app to get your location. In this post, I showed how to use two components to do the same thing. Hopefully this blog post will inspire some Delphi developers to build apps that take advantage of 2 of the hundreds of FM components to build the unique and powerful apps with Delphi XE5.

    Maybe you or someone else would post a comment with the Java code for Android and iOS?

    What example apps do you want me to write?

  • Guest
    David Intersimone Monday, 4 November 2013

    Rolf - "A useful demo for me is it to show how can I perform a lot of datasets into a listbox. I write on a ios app who had 10 or more datasets. If I show it in a listbox the application hangs for a few second. Under the vcl the program doesn’t hang if I like to show 10 or more datasets in a listbox."

    Use a ListView for displaying the results of dataset operations, not a ListBox - here are a few resource links for you and others:

    http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_LiveBindings_to_Populate_a_ListView_(iOS_and_Android)
    http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.ListView.TListView

    The FM R&D team is continuing work to provide higher performance for both ListView and ListBox components on iOS and Android.

  • Guest
    Andreas Wednesday, 11 December 2013

    Hi David,
    it's fine what we all can do with Delphi XE5, but mobile app with Android galaxy tab7 (OS 4.1.1) and Location Sensor doesn't work. No latitude or longitude will be shown.
    I compiled the Delphi sensor demo.
    is it a bug or are there some android Versions not supported?

  • Guest
    Steve Wednesday, 18 December 2013

    Hi in 14 above you talk about examples for Bluetooth and serial comms using XE5 Mobile. I for one would find guidance on using communications on Android very useful. Have any examples been produced yet?

    Thanks

  • Guest
    Alexander Blohme Saturday, 22 March 2014

    It doesnt work anymore... Google has changed its API think, All I get now is: The Google Maps Embed API must be used in an iframe

  • Please login first in order for you to submit comments

Check out more tips and tricks in this development video: