마커, 정보창 클릭 이벤트



java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.ghj.blog_039;
 
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
 
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.BitmapDescriptorFactory;
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 org.json.JSONException;
import org.json.JSONObject;
 
import java.util.HashMap;
import java.util.Map;
 
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
 
    //GoogleMap 객체
    GoogleMap googleMap;
 
    //위도 경도
    final static double mLatitude = 37.5197889;   //위도
    final static double mLongitude = 126.9403083;  //경도
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //맵생성
        SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        //콜백클래스 설정
        mapFragment.getMapAsync(this);
    }
 
    //구글맵 생성 콜백
    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
 
        //지도타입 - 일반
        this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
 
        //기본위치(63빌딩)
        LatLng position = new LatLng(mLatitude , mLongitude);
 
        //화면중앙의 위치와 카메라 줌비율
        this.googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 14));
 
        onAddMarker();
    }
 
    //마커 , 원추가
    public void onAddMarker(){
        LatLng position = new LatLng(mLatitude , mLongitude);
 
        //나의위치 마커
        MarkerOptions mymarker = new MarkerOptions()
                .icon(BitmapDescriptorFactory.defaultMarker(200f))  //마커색상지정
                .title("마커 타이틀")
                .position(position);   //마커위치
 
        //마커추가
        this.googleMap.addMarker(mymarker);
 
        //정보창 클릭 리스너
        googleMap.setOnInfoWindowClickListener(infoWindowClickListener);
 
        //마커 클릭 리스너
        this.googleMap.setOnMarkerClickListener(markerClickListener);
    }
 
    //정보창 클릭 리스너
    GoogleMap.OnInfoWindowClickListener infoWindowClickListener = new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            String markerId = marker.getId();
            Toast.makeText(MainActivity.this"정보창 클릭 Marker ID : "+markerId, Toast.LENGTH_SHORT).show();
        }
    };
 
    //마커 클릭 리스너
    GoogleMap.OnMarkerClickListener markerClickListener = new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            String markerId = marker.getId();
            //선택한 타겟위치
            LatLng location = marker.getPosition();
            Toast.makeText(MainActivity.this"마커 클릭 Marker ID : "+markerId+"("+location.latitude+" "+location.longitude+")", Toast.LENGTH_SHORT).show();
 
            return false;
        }
    };
}
 
cs



xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <fragment
        android:id="@+id/map"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>
 
</LinearLayout>
 
cs



AndroidManifest.xml)

1
2
3
<!-- Google Map -->
        <meta-data android:name="com.google.android.geo.API_KEY" android:value="" />
 
cs



gradle)

1
compile 'com.google.android.gms:play-services-maps:9.0.2'
cs



결과

 


+ Recent posts