반응형

Places API Web Service를 이용하여 현재 위치 주변에 있는 음식점 같은 장소 정보를 획득하여 주변에 위치를 아래 화면처럼 빨간색 마커로 출력해줍니다.



마커 하나를 선택하면 해당 장소의 이름과 주소를 정보 창에 보여주는데 이 정보를 다른 액티비티로 전송하는 것을 구현해보았습니다.





해당 정보 창을 선택하면





정보 박스에 있던 해당 정보를 새로운 액티비티에서 보여줍니다.





프로그래밍은 다음 포스팅의 코드를 기반으로 작성했습니다.


Places API Web Service를 사용하여 Android Google Map에 현재 위치 주변의 음식점 표시하기

http://webnautes.tistory.com/1080




onMapReady 메소드에서 정보 창을 클릭시 인텐트를 이용하여 새로운 액티비티 실행시 정보를 전달하도록 다음처럼 작성합니다.


       mGoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

           @Override
           public void onInfoWindowClick(Marker marker) {

               Intent intent = new Intent(getBaseContext(), NewActivity.class);

               String title = marker.getTitle();
               String address = marker.getSnippet();

               intent.putExtra("title", title);
               intent.putExtra( "address", address);

               startActivity(intent);
           }
       });





새로운 액티비티의 코드와 레이아웃을 작성합니다.


NewActivity.java

package com.tistory.webnautes.googlemapsandroidapiexample;

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

/**
* Created by webnautes on 2017-11-27.
*/

public class NewActivity extends AppCompatActivity {


   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.newactivity);


       String title = "";
       String address = "";

       Bundle extras = getIntent().getExtras();

       if (extras == null) {
           title = "error";
       }
       else {

           title = extras.getString("title");
           address = extras.getString("address");
       }

       TextView textView = (TextView) findViewById(R.id.textView_newActivity_contentString);

       String str = title + '\n' + address + '\n';
       textView.setText(str);

   }
}




newactivity.xml


<?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:text="TextView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/textView_newActivity_contentString" />

</LinearLayout>




MainActivity에서 NewActivity를 인식할 수 있도록 매니페스트 파일을 다음처럼 수정합니다.


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

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

       <activity android:name=".NewActivity">

       </activity>
   </application>




이제 실행시켜 보면 앞에서 설명한대로 진행이 됩니다.




참고

https://stackoverflow.com/questions/17549372/how-to-get-click-event-of-the-marker-text


반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts