First Google Maps Activity

This article is about how to make your first Google maps activity, and inside it how to surround with try/catch some piece of code, how to handle permission exception, and how to add LinearLayout for fragment. Start new project in Android studio

API17-For-Google-Maps-Activity

Use API 17 for Google maps.

Then choose Google maps activity

Google-Maps-Activity

Now, your Google maps activity is initialized.

Then paste your Google api key maps (that you must have prepared in advance) in following place app→res→values→google_maps_api.xml

Maps-API-key-code-in-XML

So far, this is an equivalent to “Hello World” application for Google maps. Let’s start with code changes. In order to present where is the wi-fi position where mobile user is connected to the internet, we need to make following changes in existing code.
First go to
app→java→cirvirlab.nativegooglemaps→MapsActivity.java and there, in the method
public void onMapReady(GoogleMap googleMap) {

add code that will enable setMyLocationEnabled property of the map.
Note: Android studio will not let you add this line of code without try/catch structure due to security reason to handle exception. Message from Android studio for this piece of code is:
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available. So add next code to handle permission exception.

try
{

mMap.setMyLocationEnabled(true);

} catch (SecurityException e)
{

Log.e(“PERMISSION_EXCEPTION”, “PERMISSION_NOT_GRANTED”);

}

try-catch-SecurityException

Now, your “Hello World” application for Google maps is working! But we need to organize the map fragment in layout in order to add other widgets to form. So go to
app→res→layout→activity_maps.xml and existing XML code surround by <LinearLayout

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

xmlns:android=”http://schemas.android.com/apk/res/android”>


</LinearLayout>

Now, we have LinearLayout for the fragment.

Android-Layout-Fragment

This gives us possibility to add plain text field.

Plain-Text-Field-to-Fragment

Let’s orientate LinearLayout to vertical

<LinearLayout

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

xmlns:android=”http://schemas.android.com/apk/res/android”

android:orientation=”vertical”>

Application now have better functionality.

Android-Layout-Fragment-Vertical

We now need one Button. To keep TextView and Button together, we need one more layout.

<LinearLayout

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

xmlns:android=”http://schemas.android.com/apk/res/android”

android:orientation=”vertical”>

<LinearLayout

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”>

Closing this layout should surround both Button and TextView. Now it looks as following:

Android-Fragment-two-Layouts

We need now to add onClick event to Button, for example clickSearch, and to add code that will find on the map location that is typed.
Get from location name will not be enabled unless we surround with try/catch geocoder.getFromLocationName.

public void clickSearch(View view)
{

EditText location = (EditText)findViewById(R.id.editText);

String str_location = location.getText().toString();

if((str_location!=null)|| !str_location.equals(“”))

{

Geocoder geocoder = new Geocoder(this);

geocoder.getFromLocationName(str_location,1);

}

}

Just click on the bulb and choose surround with try/catch.

Surround-with-try-catch-Geocoder

We need only one location, so parameter is 1,
geocoder.getFromLocationName(str_location,1);

We need list of the addresses in order to match data structure, and we need to set addressList to null so that code will work.

This is whole code:

public void clickSearch(View view)
{

EditText location = (EditText)findViewById(R.id.editText);

String str_location = location.getText().toString();

List<Address> addressList = null;

if((str_location!=null)|| !str_location.equals(“”))

{

Geocoder geocoder = new Geocoder(this);

try

{

addressList = geocoder.getFromLocationName(str_location, 1);

}

catch (IOException e)

{

e.printStackTrace();

}

Address address = addressList.get(0);

LatLng latLng = new LatLng(address.getLatitude(),address.getLongitude());

mMap.addMarker(new MarkerOptions().position(latLng).title(“Your position”));

mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

}

}

Note: in order to have tiny button to bring your map to your current location.

Screenshot-tiny-button

Fragment in layout don’t spread till end.

fragment-not-spreaded

Posted in android Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*