본문

180831(금) - Google Maps (Get Started)

Google Maps


Get Started

- Google Play services

・ Google Maps

com.google.android.gms:play-services-maps:15.0.1

・ Google Places

com.google.android.gms:play-services-places:15.0.1


- Google Maps Activity 를 사용하면 새로만들때 좀 더 쉬운듯.


Google Maps API key

- Google Maps servers에 access하기 위해서는 API key가 필요

- Android apps를 restriction할 수 있는 type의 key가 필요

The fast, easy way : Android Studio에서 create 해주는 google_maps_api.xml link를 사용

1. googles_maps_api.xml file의 link를 복사해서, browser에 붙여넣는다. 이 링크는 RUL parameters로 필요한 정보를 가지고 Google Cloud Platform Console에 연결되므로 수동으로 입력해야 하는 일을 줄일 수 있음.


2. 해당 project에 대한 Android-restricted API key create


3. resulting API key를 copy하여 google_maps_api.xml file의 <string> element에 붙여넣는다.


A slightly less fast way : Android Studio에서 created한 credentials를 사용

1. google_maps_api.xml file에서 제공하는 credentials copy


2. go to Google Cloud Platform Console 


3. copied cedentials to add your existing API key, or create new API key


The full process for getting an API key : 위의 어느 상황과도 맞지 않으면, complete process 고고


Templete code

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:id="@+id/map"
   
tools:context=".MapsActivity"
   
android:name="com.google.android.gms.maps.SupportMapFragment" />
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

   
private GoogleMap mMap;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView
(R.layout.activity_maps);
       
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
               
.findFragmentById(R.id.map);
        mapFragment
.getMapAsync(this);

   
}

   
@Override
   
public void onMapReady(GoogleMap googleMap) {
        mMap
= googleMap;

       
// Add a marker in Sydney, Australia, and move the camera.
       
LatLng sydney = new LatLng(-34, 151);
        mMap
.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap
.moveCamera(CameraUpdateFactory.newLatLng(sydney));

   
}
}

-if maps activity file이 위 코드를 포함하지 않으면, package name 뒤에 위 코드로 file contents를 바꾼다 (뭔소리여)


Connect an Android device

- AVD emulator는 4.2.2 이상에서 동작시킬 것. (min sdk version은 14)

- x86 target AVD + configuring virtual machine acceleration = improve your experience

공유

댓글