Google Maps Platform 사이트에 가면 구글에서 제공하는 현재 위치 표시 소스가 있습니다.

이걸 이용해서 핸드폰에 현재 위치를 표시해 보도록 하겠습니다.

 

순서

1. 프로젝트 생성

2. Source Code 작성

2.1 AndroidManifest.xml

2.2 activity_main.xml

2.3 custom_info_contents.xml

2.4 current_place_menu.xml

2.5 strings.xml

2.6 build.gradle (Project)

2.7 build.gradle (Module)

2.8 MainActivity.java

3. 결과

4. APK

5. 전체 Source Code

5.1 AndroidManifest.xml

5.2 activity_main.xml

5.3 custom_info_contents.xml

5.4 current_place_menu.xml

5.5 strings.xml

5.6 build.gradle (Project)

5.7 build.gradle (Module)

5.8 MainActivity.java

 

 

사이트 주소

https://developers.google.com/maps/documentation/android-sdk/current-place-tutorial

에 접속하면 Source code와 함께 자세한 설명이 나와있습니다.



< > Show/Hide the Java code for the map activity.

이글을 클릭하면 소스 전체 보기가 펼쳐 집니다.

이 소스를 이용하여 프로젝트를 생성해 보겠습니다.


최종 결과는 현재 위치 표시와 근처 가계 정보가 나옵니다.


구글맵 현재 위치 표시


 

 

1. 프로젝트 생성

 

제목을 제외하고는 모두 기본 설정을 사용 했습니다.





package명을 이용하여



Google map API키에 등록 합니다.



API Key 생성과 등록에 대한 설명이 필요하면



[안드로이드] google map 사용을 위한 API 키 생성(2018.11)


[안드로이드] google map 테스트


기존 설명을 참고 하세요.



2. Source Code 작성


google 에서 제공하는 위치 정보에는 현재 위치에 대한 지역정보를 보여주도록 되어있어서 불필요한 설정들이 있습니다.

, 지역정보가 필요 없다면 생략해도 되는 소스들이 많이 있어 Resource 설정부분이 좀 복잡합니다.

일단은 모두 기록를 하였으니 필요없는 부분은 생략해서 사용하세요.

 

2.1 AndroidManifest.xml


권한 설정을 추가 합니다.

 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.INTERNET"/>

 

API 키도 추가해 줍니다.

 

<meta-data
   
android:name="com.google.android.geo.API_KEY"
   
android:value="Axxxxxxxxxxxxxxxxxxxxxxxxxxxx_x1" />

 

<meta-data
   
android:name="com.google.android.gms.version"
   
android:value="@integer/google_play_services_version" />

 

 

2.2 activity_main.xml

 

layoutfragment를 추가 합니다.

 

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.currentplacedetailsonmap.MapsActivityCurrentPlace" />

 

2.3 custom_info_contents.xml


res>layout에 현재 위치의 지역정보를 보여주는 레이아웃 정보를 추가 합니다.

필요가 없다면 생략해도 됩니다.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:layoutDirection="locale"
   
android:orientation="vertical">
    <TextView
       
android:id="@+id/title"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:textColor="#ff000000"
       
android:textStyle="bold" />

    <TextView
       
android:id="@+id/snippet"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textColor="#ff7f7f7f" />
</LinearLayout>

 

2.4 current_place_menu.xml


res>menu에 추가 합니다.

현재 위치의 지역정보를 보여주기 위한 메뉴입니다.

필요 없다면 생략 합니다.

 

<?xml version="1.0" encoding="utf-8"?><!--
     Copyright (C) 2016 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
       
android:id="@+id/option_get_place"
       
android:title="@string/option_get_place"
       
app:showAsAction="always"/>
</menu>

 

2.5 strings.xml


메뉴등 텍스트 정보를 추가 합니다.

그냥 layout에 하드코딩 해도 됩니다.

<resources>
    <string
name="app_name">MyLocation</string>
    <string
name="default_info_title">Default Location</string>
    <string
name="default_info_snippet">No places found, because location permission is disabled.</string>
    <string
name="option_get_place">Get place</string>
    <string
name="pick_place">Choose a place</string>
</resources>

 

2.6 build.gradle (Project)


gms 추가

classpath 'com.google.gms:google-services:3.1.0'

 

2.7 build.gradle (Module)


gms 추가

 

implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.google.android.gms:play-services-maps:15.0.0'
implementation 'com.google.android.gms:play-services-location:15.0.0'
implementation 'com.google.android.gms:play-services-places:15.0.0'

 

 

2.8 MainActivity.java


google 사이트 소스를 복사 합니다.

 

작성된 코드는 원본과 세곳이 다릅니다.

 

class 명이 기본 값이라 TAG 생성시 MainActivity를 사용했습니다.

private static final String TAG = MainActivity.class.getSimpleName();

기본 좌표를 한국으로 변경했습니다.

private final LatLng mDefaultLocation = new LatLng(37.56, 126.97);

layout도 기본설정이라 activity_main으로 사용합니다.

setContentView(R.layout.activity_main);

 

 

3. 결과


주말에 돌아다니다 캡처를 해봤습니다.

우측 상단 GET PLACE를 클릭하면 주변 정보를 보여줍니다.



 

4. APK

 

소스코드 작업이 귀찮을때는 다운받아 테스트 해보세요.


MyLocation.apk




5. 전체 Source Code


android mylocation

 

5.1 AndroidManifest.xml


 

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
   
package="copycoding.tistory.mylocation">

    <uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission
android:name="android.permission.INTERNET"/>

    <application
        
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
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="Xxxxxxxxxxxxxxxxxxxxxxxxxxxx_x1" />

        <activity
android:name=".MainActivity">
            <intent-filter>
                <action
android:name="android.intent.action.MAIN" />

                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

5.2 activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">

    <fragment
xmlns:android="http://schemas.android.com/apk/res/android"
       
xmlns:tools="http://schemas.android.com/tools"
       
android:id="@+id/map"
       
android:name="com.google.android.gms.maps.SupportMapFragment"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
        
tools:context="com.example.currentplacedetailsonmap.MapsActivityCurrentPlace" />

</android.support.constraint.ConstraintLayout>

 

5.3 custom_info_contents.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:layoutDirection="locale"
   
android:orientation="vertical">
    <TextView
       
android:id="@+id/title"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:textColor="#ff000000"
       
android:textStyle="bold" />

    <TextView
       
android:id="@+id/snippet"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textColor="#ff7f7f7f" />
</LinearLayout>

 

5.4 current_place_menu.xml


<?xml version="1.0" encoding="utf-8"?><!--
     Copyright (C) 2016 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
       
android:id="@+id/option_get_place"
       
android:title="@string/option_get_place"
       
app:showAsAction="always"/>
</menu>

 

5.5 strings.xml


<resources>
    <string
name="app_name">MyLocation</string>
    <string
name="default_info_title">Default Location</string>
    <string
name="default_info_snippet">No places found, because location permission is disabled.</string>
    <string
name="option_get_place">Get place</string>
    <string
name="pick_place">Choose a place</string>
</resources>

 

5.6 build.gradle (Project)


// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
   
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath
'com.android.tools.build:gradle:3.2.1'

       
classpath 'com.google.gms:google-services:3.1.0'
       

       
// NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
   
}
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(
type: Delete) {
    delete
rootProject.buildDir
}

 

5.7 build.gradle (Module)


apply plugin: 'com.android.application'

android {
    compileSdkVersion
28
   
defaultConfig {
       
applicationId "copycoding.tistory.mylocation"
       
minSdkVersion 14
       
targetSdkVersion 28
       
versionCode 1
       
versionName "1.0"
       
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   
}
    buildTypes {
        release {
           
minifyEnabled false
           
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       
}
    }
}

dependencies {
    implementation fileTree(
dir: 'libs', include: ['*.jar'])
    implementation
'com.android.support:appcompat-v7:28.0.0'
   
implementation 'com.android.support:support-v4:27.1.1'
   
implementation 'com.google.android.gms:play-services-maps:15.0.0'
   
implementation 'com.google.android.gms:play-services-location:15.0.0'
   
implementation 'com.google.android.gms:play-services-places:15.0.0'
   
   
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   
testImplementation 'junit:junit:4.12'
   
androidTestImplementation 'com.android.support.test:runner:1.0.2'
   
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

 

5.8 MainActivity.java

 

package copycoding.tistory.mylocation;

import
android.content.DialogInterface;
import
android.content.pm.PackageManager;
import
android.location.Location;
import
android.os.Bundle;
import
android.support.annotation.NonNull;
import
android.support.v4.app.ActivityCompat;
import
android.support.v4.content.ContextCompat;
import
android.support.v7.app.AlertDialog;
import
android.support.v7.app.AppCompatActivity;
import
android.util.Log;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.widget.FrameLayout;
import
android.widget.TextView;

import
com.google.android.gms.location.FusedLocationProviderClient;
import
com.google.android.gms.location.LocationServices;
import
com.google.android.gms.location.places.GeoDataClient;
import
com.google.android.gms.location.places.PlaceDetectionClient;
import
com.google.android.gms.location.places.PlaceLikelihood;
import
com.google.android.gms.location.places.PlaceLikelihoodBufferResponse;
import
com.google.android.gms.location.places.Places;
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.CameraPosition;
import
com.google.android.gms.maps.model.LatLng;
import
com.google.android.gms.maps.model.Marker;
import
com.google.android.gms.maps.model.MarkerOptions;
import
com.google.android.gms.tasks.OnCompleteListener;
import
com.google.android.gms.tasks.Task;

import
java.util.Locale;

public class
MainActivity extends AppCompatActivity implements OnMapReadyCallback {

   
private static final String TAG = MainActivity.class.getSimpleName();
    private
GoogleMap mMap;
    private
CameraPosition mCameraPosition;

   
// The entry points to the Places API.
   
private GeoDataClient mGeoDataClient;
    private
PlaceDetectionClient mPlaceDetectionClient;

   
// The entry point to the Fused Location Provider.
   
private FusedLocationProviderClient mFusedLocationProviderClient;
   
// A default location (Sydney, Australia) and default zoom to use when location permission is
    // not granted.
   
private final LatLng mDefaultLocation = new LatLng(37.56, 126.97);
    private static final int
DEFAULT_ZOOM = 15;
    private static final int
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
    private boolean
mLocationPermissionGranted;

    private
Location mLastKnownLocation;

   
// Keys for storing activity state.
   
private static final String KEY_CAMERA_POSITION = "camera_position";
    private static final
String KEY_LOCATION = "location";

   
// Used for selecting the current place.
   
private static final int M_MAX_ENTRIES = 5;
    private
String[] mLikelyPlaceNames;
    private
String[] mLikelyPlaceAddresses;
    private
String[] mLikelyPlaceAttributions;
    private
LatLng[] mLikelyPlaceLatLngs;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
// Retrieve location and camera position from saved instance state.
       
if (savedInstanceState != null) {
           
mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
           
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
       
}
        setContentView(R.layout.
activity_main);

       
mGeoDataClient = Places.getGeoDataClient(this, null);
       
mPlaceDetectionClient = Places.getPlaceDetectionClient(this, null);
       
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

       

        // Build the map.
       
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.
map);
       
mapFragment.getMapAsync(this);
   
}

   
/**
     * Saves the state of the map when the activity is paused.
     */
   
@Override
   
protected void onSaveInstanceState(Bundle outState) {
       
if (mMap != null) {
            outState.putParcelable(
KEY_CAMERA_POSITION, mMap.getCameraPosition());
            
outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
            super
.onSaveInstanceState(outState);
       
}
    }

   
/**
     * Sets up the options menu.
     * @param
menu The options menu.
     * @return Boolean.
     */
   
@Override
   
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.
current_place_menu, menu);
        return true;
   
}

   
/**
     * Handles a click on the menu option to get a place.
     * @param
item The menu item to handle.
     * @return Boolean.
     */
   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
if (item.getItemId() == R.id.option_get_place) {
            showCurrentPlace()
;
       
}
       
return true;
   
}

   
/**
     * Manipulates the map when it's available.
     * This callback is triggered when the map is ready to be used.
     */
   
@Override
   
public void onMapReady(GoogleMap map) {
       
mMap = map;

       
// Use a custom info window adapter to handle multiple lines of text in the
        // info window contents.
       
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

           
@Override
           
// Return null here, so that getInfoContents() is called next.
           
public View getInfoWindow(Marker arg0) {
               
return null;
           
}

           
@Override
           
public View getInfoContents(Marker marker) {
               
// Inflate the layouts for the info window, title and snippet.
               
View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents,
                       
(FrameLayout) findViewById(R.id.map), false);

               
TextView title = ((TextView) infoWindow.findViewById(R.id.title));
               
title.setText(marker.getTitle());

               
TextView snippet = ((TextView) infoWindow.findViewById(R.id.snippet));
               
snippet.setText(marker.getSnippet());

                return
infoWindow;
           
}
        })
;

       
// Prompt the user for permission.
        
getLocationPermission();

       
// Turn on the My Location layer and the related control on the map.
       
updateLocationUI();

       
// Get the current location of the device and set the position of the map.
       
getDeviceLocation();
   
}

    
/**
     * Gets the current location of the device, and positions the map's camera.
     */
   
private void getDeviceLocation() {
       
/*
         * Get the best and most recent location of the device, which may be null in rare
         * cases when a location is not available.
         */
       
try {
           
if (mLocationPermissionGranted) {
                Task<Location> locationResult =
mFusedLocationProviderClient.getLastLocation();
               
locationResult.addOnCompleteListener(this, new OnCompleteListener<Location>() {
                   
@Override
                   
public void onComplete(@NonNull Task<Location> task) {
                       
if (task.isSuccessful()) {
                           
// Set the map's camera position to the current location of the device.
                           
mLastKnownLocation = task.getResult();
                           
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                                   
new LatLng(mLastKnownLocation.getLatitude(),
                                           
mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
                       
} else {
                            Log.d(
TAG, "Current location is null. Using defaults.");
                           
Log.e(TAG, "Exception: %s", task.getException());
                           
mMap.moveCamera(CameraUpdateFactory
                                    .newLatLngZoom(
mDefaultLocation, DEFAULT_ZOOM));
                           
mMap.getUiSettings().setMyLocationButtonEnabled(false);
                       
}
                    }
                })
;
           
}
        }
catch (SecurityException e)  {
            Log.e(
"Exception: %s", e.getMessage());
       
}
    }


   
/**
     * Prompts the user for permission to use the device location.
     */
   
private void getLocationPermission() {
       
/*
         * Request location permission, so that we can get the location of the
         * device. The result of the permission request is handled by a callback,
         * onRequestPermissionsResult.
         */
       
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
               
android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.
PERMISSION_GRANTED) {
           
mLocationPermissionGranted = true;
       
} else {
            ActivityCompat.requestPermissions(
this,
                    new
String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                   
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
       
}
    }

   
/**
     * Handles the result of the request for location permissions.
     */
   
@Override
   
public void onRequestPermissionsResult(int requestCode,
                                          
@NonNull String permissions[],
                                           
@NonNull int[] grantResults) {
       
mLocationPermissionGranted = false;
        switch
(requestCode) {
           
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
               
// If request is cancelled, the result arrays are empty.
               
if (grantResults.length > 0
                       
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   
mLocationPermissionGranted = true;
               
}
            }
        }
        updateLocationUI()
;
   
}

   
/**
     * Prompts the user to select the current place from a list of likely places, and shows the
     * current place on the map - provided the user has granted location permission.
     */
   
private void showCurrentPlace() {
       
if (mMap == null) {
           
return;
       
}

       
if (mLocationPermissionGranted) {
           
// Get the likely places - that is, the businesses and other points of interest that
            // are the best match for the device's current location.
           
@SuppressWarnings("MissingPermission") final
           
Task<PlaceLikelihoodBufferResponse> placeResult =
                   
mPlaceDetectionClient.getCurrentPlace(null);
           
placeResult.addOnCompleteListener
                    (
new OnCompleteListener<PlaceLikelihoodBufferResponse>() {
                       
@Override
                       
public void onComplete(@NonNull Task<PlaceLikelihoodBufferResponse> task) {
                           
if (task.isSuccessful() && task.getResult() != null) {
                                PlaceLikelihoodBufferResponse likelyPlaces = task.getResult()
;

                               
// Set the count, handling cases where less than 5 entries are returned.
                                
int count;
                                if
(likelyPlaces.getCount() < M_MAX_ENTRIES) {
                                    count = likelyPlaces.getCount()
;
                               
} else {
                                    count =
M_MAX_ENTRIES;
                               
}

                               
int i = 0;
                               
mLikelyPlaceNames = new String[count];
                               
mLikelyPlaceAddresses = new String[count];
                                
mLikelyPlaceAttributions = new String[count];
                               
mLikelyPlaceLatLngs = new LatLng[count];

                                for
(PlaceLikelihood placeLikelihood : likelyPlaces) {
                                   
// Build a list of likely places to show the user.
                                   
mLikelyPlaceNames[i] = (String) placeLikelihood.getPlace().getName();
                                   
mLikelyPlaceAddresses[i] = (String) placeLikelihood.getPlace()
                                            .getAddress()
;
                                   
mLikelyPlaceAttributions[i] = (String) placeLikelihood.getPlace()
                                            .getAttributions()
;
                                    
mLikelyPlaceLatLngs[i] = placeLikelihood.getPlace().getLatLng();

                                   
i++;
                                    if
(i > (count - 1)) {
                                       
break;
                                   
}
                                }

                               
// Release the place likelihood buffer, to avoid memory leaks.
                               
likelyPlaces.release();

                               
// Show a dialog offering the user the list of likely places, and add a
                                // marker at the selected place.
                               
openPlacesDialog();

                           
} else {
                                Log.e(
TAG, "Exception: %s", task.getException());
                           
}
                        }
                    })
;
       
} else {
           
// The user has not granted permission.
           
Log.i(TAG, "The user did not grant location permission.");

           
// Add a default marker, because the user hasn't selected a place.
           
mMap.addMarker(new MarkerOptions()
                    .title(getString(R.string.
default_info_title))
                    .position(
mDefaultLocation)
                    .snippet(getString(R.string.
default_info_snippet)));

           
// Prompt the user for permission.
           
getLocationPermission();
       
}
    }

   
/**
     * Displays a form allowing the user to select a place from a list of likely places.
     */
   
private void openPlacesDialog() {
       
// Ask the user to choose the place where they are now.
       
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
           
@Override
           
public void onClick(DialogInterface dialog, int which) {
               
// The "which" argument contains the position of the selected item.
               
LatLng markerLatLng = mLikelyPlaceLatLngs[which];
               
String markerSnippet = mLikelyPlaceAddresses[which];
                if
(mLikelyPlaceAttributions[which] != null) {
                    markerSnippet = markerSnippet +
"\n" + mLikelyPlaceAttributions[which];
               
}

               
// Add a marker for the selected place, with an info window
                // showing information about that place.
               
mMap.addMarker(new MarkerOptions()
                        .title(
mLikelyPlaceNames[which])
                        .position(markerLatLng)
                        .snippet(markerSnippet))
;

               
// Position the map's camera at the location of the marker.
               
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(markerLatLng,
                       
DEFAULT_ZOOM));
           
}
        }
;

       
// Display the dialog.
       
AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle(R.string.
pick_place)
                .setItems(
mLikelyPlaceNames, listener)
                .show()
;
   
}

   
/**
     * Updates the map's UI settings based on whether the user has granted location permission.
     */
   
private void updateLocationUI() {
       
if (mMap == null) {
           
return;
       
}
       
try {
           
if (mLocationPermissionGranted) {
               
mMap.setMyLocationEnabled(true);
               
mMap.getUiSettings().setMyLocationButtonEnabled(true);
           
} else {
               
mMap.setMyLocationEnabled(false);
               
mMap.getUiSettings().setMyLocationButtonEnabled(false);
               
mLastKnownLocation = null;
               
getLocationPermission();
           
}
        }
catch (SecurityException e)  {
            Log.e(
"Exception: %s", e.getMessage());
       
}
    }
}

 

- copy coding - 


구글 맵이 유료로 변경되면서 뭔가 새로운 변화가 있나 확인도 할겸 google map 서비스를 다시 사용해 봅니다.

처음부터 프로젝트를 만들고 키 값을 설정하는 부분 까지만 진행 하고 실제 안드로이드에서 맵을 사용 하는 부분은 따로 작업을 하도록 하겠습니다.

키 값을 생성 하기 위해 프로젝트를 생성 하려면 로그인을 해야 하기 때문에 회원 가입이 되어 있어야 합니다.

 


1. 프로젝트 생성

 

1.1 프로젝트 명 만들기


Google Developers Console 사이트 (https://console.developers.google.com )에 접속합니다.

현재 신규 아이디로 로그인만 하고 아무것도 생성되지 않은 상태 입니다.


android api key


이제 처음부터 시작을 하기 위해 [프로젝트 만들기] 버튼을 클릭 합니다.



대시보드에서 [만들기] 버튼을 클릭 합니다.

사용할 프로젝트 이름을 생성하는 화면 입니다.



프로젝트 이름은 편한대로 생성 하는데 성격은 포함되도록

MyMapProj

저는 이렇게 하고 [만들기] 버튼을 클릭합니다.

 

잠시 기다리면 다시 프로젝트가 생성 되고 첫 화면으로 돌아 갑니다.

화면이 처음과 비슷해서 아무 일도 안 일어난것 같지만 상단에 보면 신규 프로젝트가 생성되어 있습니다.


첫 화면과 비교해 볼까요?

상단 조직 없음에서 프로젝트 이름 MyMapProj 로 변경이 되어 있습니다.




 

한번 뭐가 있나 MyMapProj를 눌러 봅니다.

 


별거 없군요. 누를 필요 없겠습니다.



1.2 구글 맵 서비스 등록

 

이제 생성된 프로젝트에서 사용할 API를 등록해야 합니다.

그러기 위해서는 서비스를 등록 해야 하겠지요.



[API 및 서비스 사용 설정]을 선택 합니다.


 

종류가 많으므로 검색 키워드를 입력 합니다.

map만 입력하면 너무 많이 나오니까 map android를 입력하고 검색 합니다.

 


안드로이드 앱 개발을 테스르 하기 위해 처음에 있는 Maps SDK for Android를 선택 합니다.



 

이제 [사용 설정] 버튼을 클릭합니다.

 

~~~~~! 지겨워 한 페이지에서 다 선택 하게 하면 좋을 텐데 페이지가 너무 많네요.



화면이 다시 메인으로 넘어 오는데 예전에 못보던 할당량 이라는 메뉴가 들어 있습니다.

이게 유료화와 관련이 있나? 한번 눌러 봤습니다.



일단 여기까지. 자세한건 나중에 천천히 보고 지금 해야 하는건 사용자 인증 정보를 만드는 것입니다.

 


2. 사용자 인증 정보

 

2.1 인증 키 생성


할당량 옆에 있는 [사용자 인증 정보]를 선택 합니다.



[사용자 인증 정보 만들기]를 선택 합니다.

 

몇가지 선택 할 수 있는 메뉴가 나옵니다.




goole map을 사용하기 위한 키를 생성하는 것이니

[API ] 를 선택 합니다.


잠시 생성중... 하다가 키를 하나 만들어서 보여 줍니다.


여기까지가 구글 맵을 사용하기 위한 키값 생성하는 작업 입니다.




? 필요 없으시다 구요?



그럼 [사용중지]나 쓰레기통을 클릭하세요.


계속 진행 하려면 [API] 또는 연필 아이콘을 클릭 합니다.

 

2.2 앱과 개발 장비 등록 화면

 

여기서 잠깐!

이 글을 찾아서 보시는 분이라면 처음 사용하는 분이라 생각됩니다.

여기에 있는 키가 안드로이드 스튜디오에서 구글 맵을 추가할 때 사용하는 인증 키 입니다.

아래에서 설명 하는 내용은 이 키 값을 가지고 만들게 되는 앱과 컴퓨터 정보를 등록하는 부분입니다.


 

자신의 용도에 맞게 라디오 버튼을 선택 하고 [저장] 버튼을 클릭 합니다.

 

Android 앱에서 사용하려고 하는 거라 Android 라디오 버튼을 누르면 하단에 지문 추가 항목이 나타납니다.


 

패키지 이름은 자신이 만들려는 앱의 패키지 이름입니다.

SHA-1 인증서 지문은 앱 개발을 하는 컴퓨터의 인증서 지문을 말합니다.

, 안드로이드 스튜디오로 생성할 프로젝트를 등록하고 컴퓨터에서 생성한 인증서도 등록 합니다.

 

여기서 부터는 잠시 웹을 중단하고 자신의 컴퓨터에서 작업을 시작 합니다.

잠시 휴식...


2.3 SHA-1 인증서 지문 생성


keytool 명령을 사용해서 인증서를 생성 하는데 안된다면 keytool이 어디 있는지 확인 합니다.

java를 어디에 설치 했는가에 따라 다르겠지만

C:\Java\jdk1.8.0_131\bin 이곳에 있습니다.

console창의 위치를 이동 하거나 path를 잡아서 명령을 실행 합니다.


콘솔 창을 하나 열어서 키를 생성 해 보도록 합니다.


c:\>keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android



그림의  SHA1이 인증서 지문 입니다.


 

2.4 패키지 이름 생성

 

이젠 안드로이드 프로젝트를 하나 생성 합니다.

MapTest 이라는 이름으로 생성을 하겠습니다.

프로젝트 명을 입력하는 화면 하단에 Package name이 있습니다.

com.example.컴퓨터이름.프로젝트명 이런식으로 나타나는데 [Edit] 버튼을 누르면 편집이 가능 합니다.



필요하면 편집을 하고 [Done] 버튼을 누르면 반영이 됩니다.



패키지 이름을 변경하려고 합니다.

com.android.google.maptest 이렇게 변경을 해 보았습니다.



2.4 API 키에 패키지 이름 및 인증서 지문 등록

 

패키지 이름과 SHA-1 인증서 지문이 모두 준비 되었습니다.

이제 웹 작업을 마무리 할 때가 되었습니다.

 


다시 웹 페이지로 돌아 갑니다.

깜짝 놀랄 수도 있지만 이해를 돕고자 예전에 만들어 놓은 인증정보를 가지고 설명을 하겠습니다.



인증서 지문이 다양하게 있죠?

일부는 노트북에서 인증서 지문을 만들고 앱을 개발한 내용이고

일부는 데스크 탑에서 인증서 지문을 만들고 앱을 개발한 내용입니다.


컴퓨터 마다 또는 생성 할 때 마다 다르게 만들어집니다.

 

빨간색 박스가 이번에 만든 패키지와 인증서 지문 입니다.


API 키는 개인에게 부여된 google map을 사용하기 위한 하나의 키 값이고 컴퓨터 마다 패키지와 인증서 지문을 등록 하는 것 입니다.

그렇다고 프로젝트를 만들 때마다 인증서 지문을 만드는건 아닙니다.

위에서 보듯이 동일한 인증서 지문에 패키지 이름(새로 생성한 프로젝트)을 입력하고 [저장]버튼을 누르면 됩니다.

새로 프로젝트를 만들면 패키지명은 새로 추가되고 인증서지문은 동일한걸 사용하면 됩니다.



요 빨간색 박스의 키가 개인에게 부여된 키이고 위에서 한 작업은 프로젝트와 작업 컴퓨터를 키에 등록한다고 생각하면 됩니다.

 

키 값은 개인에게 부여된 값이기 때문에 구글 맵 사용이 유료화가 되었으니 중요도가 더 높아졌습니다.

잘 보관하여 사용 하세요.

 

다음에는 키 값을 가지고 구글 맵을 한번 생성해 보겠습니다.


- copy coding -


1

+ Recent posts