본문

180904(화) - Google Maps (Map with a Marker)

Google Maps


Map with a Marker

- map include marker(pin)


Sample code

Clone or download the Google Maps Android API v2 Samples repository 

- map activity

package com.example.mapwithmarker;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

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;

/**
 * An activity that displays a Google map with a marker (pin) to indicate a particular location.
 */

public class MapsMarkerActivity extends AppCompatActivity
       
implements OnMapReadyCallback {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
// Retrieve the content view that renders the map.
        setContentView
(R.layout.activity_maps);
       
// Get the SupportMapFragment and request notification
       
// when the map is ready to be used.
       
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
               
.findFragmentById(R.id.map);
        mapFragment
.getMapAsync(this);
   
}

   
/**
     * Manipulates the map when it's available.
     * The API invokes this callback when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user receives a prompt to install
     * Play services inside the SupportMapFragment. The API invokes this method after the user has
     * installed Google Play services and returned to the app.
     */

   
@Override
   
public void onMapReady(GoogleMap googleMap) {
       
// Add a marker in Sydney, Australia,
       
// and move the map's camera to the same location.
       
LatLng sydney = new LatLng(-33.852, 151.211);
        googleMap
.addMarker(new MarkerOptions().position(sydney)
               
.title("Marker in Sydney"));
        googleMap
.moveCamera(CameraUpdateFactory.newLatLng(sydney));
   
}
}


Add a map

1. add Fragment element to your activity's layout file

SupportMapFragment

<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.mapwithmarker.MapsMarkerActivity" />

2. Fragment setting  후 getMapAsync() register for the map callback

@Override
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
   
// Retrieve the content view that renders the map.
    setContentView
(R.layout.activity_maps);
   
// Get the SupportMapFragment and request notification
   
// when the map is ready to be used.
   
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
           
.findFragmentById(R.id.map);
    mapFragment
.getMapAsync(this);

}

3. implement OnMapReadyCallback하고, GoogleMap object is available일 때, set up

public class MapsMarkerActivity extends AppCompatActivity
       
implements OnMapReadyCallback {
   
// Include the OnCreate() method here too, as described above.
   
@Override
   
public void onMapReady(GoogleMap googleMap) {
       
// Add a marker in Sydney, Australia,
       
// and move the map's camera to the same location.
       
LatLng sydney = new LatLng(-33.852, 151.211);
        googleMap
.addMarker(new MarkerOptions().position(sydney)
               
.title("Marker in Sydney"));
        googleMap
.moveCamera(CameraUpdateFactory.newLatLng(sydney));
   
}
}


- 기본적으로 Maps SDK는 user가 marker를 tap할 때, displays content

- default behavior 도 괜찮다면 no need add a click listener

공유

댓글