ソースを参照

Integrate Google Map with the app

Yiupang 9 年 前
コミット
5bc5ba80f5

+ 4 - 1
app/build.gradle

@@ -7,6 +7,7 @@ android {
         applicationId "com.example.yiupang.freefoodfinder"
         minSdkVersion 18
         targetSdkVersion 25
+        multiDexEnabled true
         versionCode 1
         versionName "1.0"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -20,14 +21,16 @@ android {
 }
 
 dependencies {
-    compile fileTree(dir: 'libs', include: ['*.jar'])
+    compile fileTree(include: ['*.jar'], dir: 'libs')
     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
         exclude group: 'com.android.support', module: 'support-annotations'
     })
     compile 'com.android.support:appcompat-v7:25.3.1'
+    compile 'com.android.support:multidex:1.0.1'
     compile 'com.android.support.constraint:constraint-layout:1.0.2'
     compile 'com.android.support:design:25.3.1'
     compile 'com.roughike:bottom-bar:1.3.3'
     compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
     testCompile 'junit:junit:4.12'
+    compile 'com.google.android.gms:play-services-maps:10.2.4'
 }

+ 7 - 0
app/src/main/AndroidManifest.xml

@@ -2,6 +2,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.yiupang.freefoodfinder">
 
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
@@ -9,6 +10,12 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
+        <meta-data
+            android:name="com.google.android.gms.version"
+            android:value="@integer/google_play_services_version" />
+        <meta-data
+            android:name="com.google.android.geo.API_KEY"
+            android:value="@string/google_maps_key" />
         <activity
             android:name=".MainActivity"
             android:label="@string/app_name"

+ 36 - 3
app/src/main/java/com/example/yiupang/freefoodfinder/MapScreen.java

@@ -1,20 +1,53 @@
 package com.example.yiupang.freefoodfinder;
 
-import android.app.Fragment;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
+import com.google.android.gms.maps.CameraUpdateFactory;
+import com.google.android.gms.maps.GoogleMap;
+import com.google.android.gms.maps.OnMapReadyCallback;
+import com.google.android.gms.maps.SupportMapFragment;
+import com.google.android.gms.maps.model.LatLng;
+import com.google.android.gms.maps.model.MarkerOptions;
+
 /**
  * Created by yiupang on 5/6/2017.
+ *
+ *
  */
 
-public class MapScreen extends android.support.v4.app.Fragment
+public class MapScreen extends android.support.v4.app.Fragment implements OnMapReadyCallback
 {
+    @Override
     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
     {
-        return inflater.inflate(R.layout.map_screen, container, false);
+        View view = inflater.inflate(R.layout.map_screen, container, false);
+        android.support.v4.app.FragmentManager fragmentManager = getChildFragmentManager();
+        SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
+        mapFragment.getMapAsync(this);
+        return view;
+    }
+
+/*    @Override
+    public void onCreate(Bundle savedInstanceState)
+    {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.map_screen);
+    }*/
+
+    @Override
+    public void onMapReady(GoogleMap map)
+    {
+        // Add a marker in Sydney and move the camera
+        LatLng SLO = new LatLng(35.2827778, -120.6586111);
+        LatLng SF = new LatLng(37.774929, -122.419416);
+        LatLng theP = new LatLng(35.302833, -120.651662);
+        map.addMarker(new MarkerOptions().position(SLO).title("Downtown").snippet("Downtownd of SLO"));
+        map.addMarker(new MarkerOptions().position(theP).title("The P").snippet("The “P” is a 50-by-30 foot landmark located atop a northwestern hill of California Polytechnic State University, San Luis Obispo, California."));
+        map.addMarker(new MarkerOptions().position(SF).title("Marker in SF").snippet("Golden Gate Bridge"));
+        map.moveCamera(CameraUpdateFactory.newLatLngZoom(SLO, 12.0f));
     }
 }

+ 5 - 13
app/src/main/res/layout/map_screen.xml

@@ -1,14 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical" android:layout_width="match_parent"
-    android:layout_height="match_parent">
-
-    <TextView android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:text="Map"
-        android:typeface="serif"
-        android:textStyle="bold"
-        android:textSize="30dp">
-
-    </TextView>
-</LinearLayout>
+<fragment xmlns:android="http://schemas.android.com/apk/res/android"
+    android:name="com.google.android.gms.maps.SupportMapFragment"
+    android:id="@+id/map"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"/>

+ 24 - 0
app/src/main/res/values/google_maps_api.xml

@@ -0,0 +1,24 @@
+<resources>
+    <!--
+    TODO: Before you run your application, you need a Google Maps API key.
+
+    To get one, follow this link, follow the directions and press "Create" at the end:
+
+    https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=95:4F:C8:4F:14:2E:31:5A:14:C0:E8:1A:5B:8A:9C:5B:F2:7C:1F:47%3Bcom.example.yiupang.googlemappractice
+
+    You can also add your credentials to an existing key, using these values:
+
+    Package name:
+    95:4F:C8:4F:14:2E:31:5A:14:C0:E8:1A:5B:8A:9C:5B:F2:7C:1F:47
+
+    SHA-1 certificate fingerprint:
+    95:4F:C8:4F:14:2E:31:5A:14:C0:E8:1A:5B:8A:9C:5B:F2:7C:1F:47
+
+    Alternatively, follow the directions here:
+    https://developers.google.com/maps/documentation/android/start#get-key
+
+    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
+    string in this file.
+    -->
+    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDs7uc0FJ4B7jLEunMB1EG3GjVKqdgL76Q</string>
+</resources>