Updated 22 December 2016
In this blog,
I have shown you how to add the google map in your application step by step.
Android allows us to integrate google maps in our application to show any shop, routes, restaurants, location etc. on the map.
Google made new Maps V2 API as a part of Google Play Services SDK.
Step 1: Add the play service library into your project build.gradle
1 compile 'com.google.android.gms:play-services-maps:8.3.0'
Step 2: After adding the library you can use Google Map – Layout file where you want to display the map
1 2 3 4 5 |
<fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> |
Step 3: Add some permissions with the Google Map API key in the AndroidManifest.XML file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!--Permissions--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application <!--Google MAP API key--> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your google api key" /> <!-- Required after latest Google Play Services update --> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> |
Step 4: Get the Api key from here.
For generating the API key you have to create a new project or select the existing one
After selecting the project you have to select Android on the add credentials to your project section
After fill the all the requirments you get the API key from google.
You can also you the existing API key.
Step 5: Initilize the Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
private void initilizeMap() { if (googleMap == null) { googleMap = ((MapFragment) getFragmentManager().findFragmentById( R.id.map)).getMap(); UiSettings mapSettings; mapSettings = googleMap.getUiSettings(); mapSettings.setZoomControlsEnabled(true); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } // check if map is created successfully or not if (googleMap == null) { Toast.makeText(getApplicationContext(), R.string.sorry_unable_to_create_maps, Toast.LENGTH_SHORT) .show(); } } } |
Step 6:Â Adding Markers
You can create markers on the map via the Marker
class.
1 2 |
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude1, longitute1)).title(address); Marker m=googleMap.addMarker(marker); |
Step 7: Zoom Camera
1 2 3 4 |
//for zoom level modify bound CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 6f); // for move the camera googleMap.moveCamera(cu); |
This is method newLatLngZoom(LatLng latLng, float zoom).
Step 8:Â If You want then can animate the map
1 |
googleMap.animateCamera(cu); |
In this example, I have taken three country codes and display it on google map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
if (googleMap == null) initilizeMap(); try { String country[] = { "UK", "IN", "IS" }; for (int i = 0; i < 3; i++) { getLatLong(this.getLocationInfo(country[i])); // create marker MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude1, longitute1)) .title(country[i]); Marker m = googleMap.addMarker(marker); LatLngBounds.Builder builder = new LatLngBounds.Builder(); builder.include(m.getPosition()); //for zoom level modify bound CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 3 f); googleMap.moveCamera(cu); } } catch (Exception e) { Log.d("Exception in marking location in map", e.getMessage()); } //Method for Location Information public static JSONObject getLocationInfo(String address) { StringBuilder responseStrBuilder = new StringBuilder(); address = address.replaceAll(" ", "%20"); try { java.net.URL url = new URL("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String inputStr; while ((inputStr = streamReader.readLine()) != null) responseStrBuilder.append(inputStr); } catch (IOException e) { e.printStackTrace(); } JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(responseStrBuilder.toString()); } catch (JSONException e) { e.printStackTrace(); } Log.d("jsonObject", jsonObject + ""); return jsonObject; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Method for getting latitude and longitute public static boolean getLatLong(JSONObject jsonObject) { try { longitute1 = ((JSONArray) jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lng"); latitude1 = ((JSONArray) jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lat"); } catch (JSONException e) { return false; } return true; } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.