Advertisement
ali50mahmoud

maps_api

Aug 21st, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. Hi, Today I am going to explain how to use Google Map API v3 in Android Application. Before going to start this tutorial we will go through some key elements of Google Map. This steps are necessary to configure a development environment to work with Google Maps and then work through some code examples demonstrating some of the basics of Google Maps Android integration.
  2.  
  3. GoogleMap – It is the main class of the Google Maps Android API. This class is responsible for downloading and displaying map tiles and for displaying and responding to map controls.
  4.  
  5. MapView - A subclass of the View class, provides the view canvas onto which the map is drawn by the Google Map object.
  6.  
  7. SupportMapFragment – A subclass of the Fragment class, allows a map to be placed within a Fragment in an Android layout.
  8.  
  9. Marker – The purpose of the Marker class is to allow locations to be marked on a Map.
  10.  
  11.  
  12. Prerequisites:
  13.  
  14. JDK 7.0 or above
  15. Android Studio 2.0
  16.  
  17.  
  18. Steps to follow:
  19.  
  20. Create a New Android Studio project name MapDemo.
  21. Select minimum API level 19 (KitKat) to Support Google Maps API v3 to Support Maximum of Android device available in Google play.
  22. Select Google MapsActivity and your Activity name is MapsActivity click next.
  23. Give your layout xml name is activity_map_demo And click finish
  24.  
  25. 5. Now to Obtain Developer signature go to google_maps_api.xml from app -> res -> values folder of the project . google_maps_api.xml file is a link to the Google Developer console.Copy and paste this link into a browser window. Once loaded, a page similar to the following will appear:
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. 6. Verify that the menu is set to Create a new project before clicking on the Continue button.Once the API has been enabled, click on the Go to Credentials button.
  33. After create a new Project a panel will appear providing the option to create an Android key for the application like below.
  34.  
  35.  
  36.  
  37.  
  38.  
  39. 7. Check that the SHA-1 fingerprint and application package name match those listed in the google_maps_api.xml file and click on the Create button to generate the API key for your application. A dialog window will subsequently appear containing the API key for the app. Copy this key, return to Android Studio and paste the API key into the
  40. YOUR_KEY_HERE section of the file:
  41.  
  42. <string name=“google_maps_key” templateMergeStrategy=“preserve”>
  43. YOUR_KEY_HERE
  44. </string>
  45.  
  46.  
  47.  
  48. 8. Now within MapsAcivity.java write the following code .
  49.  
  50. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  51.  
  52.  
  53. private GoogleMap mMap;
  54. private static final int LOCATION_REQUEST_CODE = 101;
  55. private String TAG = "MapDemo";
  56.  
  57. @Override
  58. protected void onCreate(Bundle savedInstanceState) {
  59. super.onCreate(savedInstanceState);
  60. setContentView(R.layout.activity_map_demo);
  61.  
  62. requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
  63. LOCATION_REQUEST_CODE);
  64. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  65. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  66. .findFragmentById(R.id.map);
  67. mapFragment.getMapAsync(this);
  68. }
  69.  
  70.  
  71. protected void requestPermission(String permissionType, int
  72. requestCode) {
  73. int permission = ContextCompat.checkSelfPermission(this,
  74. permissionType);
  75. if (permission != PackageManager.PERMISSION_GRANTED) {
  76. ActivityCompat.requestPermissions(this,
  77. new String[]{permissionType}, requestCode
  78. );
  79. }
  80. }
  81.  
  82.  
  83. @Override
  84. public void onRequestPermissionsResult(int requestCode, String permissions[], int[]
  85. grantResults) {
  86. switch (requestCode) {
  87. case LOCATION_REQUEST_CODE: {
  88. if (grantResults.length == 0
  89. || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
  90. Toast.makeText(this, "Unable to show location - permission required", Toast.LENGTH_LONG).show();
  91. }
  92. return;
  93. }
  94. }
  95. }
  96.  
  97.  
  98. /**
  99. * Manipulates the map once available.
  100. * This callback is triggered when the map is ready to be used.
  101. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  102. * we just add a marker near Sydney, Australia.
  103. * If Google Play services is not installed on the device, the user will be prompted to install
  104. * it inside the SupportMapFragment. This method will only be triggered once the user has
  105. * installed Google Play services and returned to the app.
  106. */
  107. @Override
  108. public void onMapReady(GoogleMap googleMap) {
  109. mMap = googleMap;
  110.  
  111.  
  112. // Add a marker in Sydney and move the camera
  113. LatLng sydney = new LatLng(-34, 151);
  114. mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
  115. mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  116.  
  117. }
  118. }
  119.  
  120.  
  121.  
  122.  
  123. 9. And Configure your Manifest file is like below.
  124.  
  125. <?xml version="1.0" encoding="utf-8"?>
  126. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  127. package="com.tutorial.mapdemo">
  128.  
  129. <!--
  130. The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
  131. Google Maps Android API v2, but you must specify either coarse or fine
  132. location permissions for the 'MyLocation' functionality.
  133.  
  134. -->
  135.  
  136. <uses-permission android:name="android.permission.INTERNET" />
  137. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  138. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  139. <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
  140. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement