• We just launched and are currently in beta. Join us as we build and grow the community.

Using Google Maps in your Android program

Hstartech

Mining Pool Operator
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
33
Likes
59
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Now the most part of Android devices have installed Google Maps Application by default. You can use Google Maps in your application for showing different locations.
Let's create a new Android Project. The application will consist of some buttons that will show different locations using different ways.
The first step is to create the layout of the application:
  1. <

    RelativeLayout xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  2. xmlns:

    tools=

    "http://schemas.android.com/tools"
  3. android:

    layout_width=

    "match_parent"
  4. android:

    layout_height=

    "match_parent"
  5. tools:

    context=

    "${relativePackage}.${activityClass}"

    >

  6. <

    Button


  7. android:

    id=

    "@+id/London"
  8. android:

    layout_width=

    "wrap_content"
  9. android:

    layout_height=

    "wrap_content"
  10. android:

    layout_below=

    "@+id/Earth"
  11. android:

    layout_centerHorizontal=

    "true"
  12. android:

    layout_marginTop=

    "42dp"
  13. android:

    text=

    "Show London"

    />

  14. <

    Button


  15. android:

    id=

    "@+id/Earth"
  16. android:

    layout_width=

    "wrap_content"
  17. android:

    layout_height=

    "wrap_content"
  18. android:

    layout_alignLeft=

    "@+id/London"
  19. android:

    layout_alignParentTop=

    "true"
  20. android:

    layout_alignRight=

    "@+id/London"
  21. android:

    layout_marginTop=

    "24dp"
  22. android:

    text=

    "Show Earth"

    />

  23. <

    Button


  24. android:

    id=

    "@+id/mc"
  25. android:

    layout_width=

    "wrap_content"
  26. android:

    layout_height=

    "wrap_content"
  27. android:

    layout_centerHorizontal=

    "true"
  28. android:

    layout_centerVertical=

    "true"
  29. android:

    text=

    "Show New Yourk McDonald's"

    />

  30. </

    RelativeLayout>

The goal of this program is not to create an application with a excellent User Interface. The main task is to test the possibilities of the using Google Maps in your program to show locations. That's way your layout should look something like this:
111.jpg


Now let's initialize all the buttons in the activity to be able to add click listener to them. First of all add these fields to your Activity class
  1. Button

    Earth;
  2. Button

    London;
  3. Button

    McDonalds;

After this you can initialize the buttons in the onCreate method:
  1. Africa =

    (

    Button

    )

    findViewById(

    R.id

    .Africa

    )

    ;
  2. London =

    (

    Button

    )

    findViewById(

    R.id

    .London

    )

    ;
  3. McDonalds =

    (

    Button

    )

    findViewById(

    R.id

    .mc

    )

    ;

And now we can add the listeners to all of the buttons. The onClickListeners are implemented as the anonymous classes. The first example is to show the Earth continent:
  1. Africa.setOnClickListener

    (

    new

    OnClickListener(

    )

    {

  2. @Override
  3. public

    void

    onClick(

    View

    v)

    {
  4. String

    locationString =

    "geo:0,10?z=2"

    ;
  5. Uri locationUri =

    Uri.parse

    (

    locationString)

    ;
  6. Intent googleMap =

    new

    Intent(

    Intent.ACTION_VIEW

    , locationUri)

    ;
  7. startActivity(

    googleMap)

    ;
  8. }

  9. }

    )

    ;

Now let's take a look on the location string. It has the following format:
  1. geo:

    latitude,longitude?

    z=

    zoom

The first two parameters are clear.These are the coordinates of the place what you want show. The last one is the zoom parameter - the maximal value for zoom is 23 and the minimum value is 2.
You can see that there is not always a good idea to use coordinates. Let's show the London by simply specifying the name of the city:
  1. London.setOnClickListener

    (

    new

    OnClickListener(

    )

    {

  2. @Override
  3. public

    void

    onClick(

    View

    v)

    {
  4. String

    locationString =

    "geo:0,0?q=London"

    ;
  5. Uri locationUri =

    Uri.parse

    (

    locationString)

    ;
  6. Intent googleMap =

    new

    Intent(

    Intent.ACTION_VIEW

    , locationUri)

    ;
  7. startActivity(

    googleMap)

    ;
  8. }

  9. }

    )

    ;

screenshot_2014-09-11-23-13-20.png

You can see that the format of the location string is now much more clear:
  1. geo:

    0

    ,0

    ?

    q=

    street+

    address

You can specify any correct street address to be shown.
Also, you can specify a search query for this format. Let's find McDonald's In New York:
  1. McDonalds.setOnClickListener

    (

    new

    OnClickListener(

    )

    {

  2. @Override
  3. public

    void

    onClick(

    View

    v)

    {
  4. String

    locationString =

    "geo:0,0?q=New Your McDonald\'

    s"

    ;
  5. Uri locationUri =

    Uri.parse

    (

    locationString)

    ;
  6. Intent googleMap =

    new

    Intent(

    Intent.ACTION_VIEW

    , locationUri)

    ;
  7. startActivity(

    googleMap)

    ;
  8. }

  9. }

    )

    ;

screenshot_2014-09-11-23-13-44.png

Now you need some basic things how to show different locations from your application. The Google Maps can be integrated in the Android application without starting the Google Maps application. But it's another theme for another article.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top