본문 바로가기
안드로이드 자바

[Java][Android] 암시적 인텐트로 지도 활용하기

by teamnova 2025. 5. 27.
728x90

안녕하세요

오늘은 암시적 인텐트로 지도를 활용해보도록 하겠습니다

 

전체 코드입니다

 

activity_main.xml

<?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"
  android:orientation="vertical"
  android:padding="24dp"
  android:gravity="center">

  <Button
    android:id="@+id/btnOpenMapLocation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="지도에서 위치 보기 (서울시청)" />

  <Button
    android:id="@+id/btnSearchInMap"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="지도에서 '카페' 검색하기"
    android:layout_marginTop="16dp" />

</LinearLayout>

 

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

  Button btnOpenMapLocation, btnSearchInMap;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnOpenMapLocation = findViewById(R.id.btnOpenMapLocation);
    btnSearchInMap = findViewById(R.id.btnSearchInMap);

    // 좌표 기반 지도 열기 (서울 시청)
    btnOpenMapLocation.setOnClickListener(v -> {
      Uri gmmIntentUri = Uri.parse("geo:37.5665,126.9780");  // 위도, 경도
      Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
      mapIntent.setPackage("com.google.android.apps.maps");  // 구글 지도
      startActivity(mapIntent);
    });

    // 키워드 기반 검색 실행 (예: 카페)
    btnSearchInMap.setOnClickListener(v -> {
      Uri gmmIntentUri = Uri.parse("geo:0,0?q=카페");  // 현재 위치 기반 검색
      Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
      mapIntent.setPackage("com.google.android.apps.maps");  // 구글 지도
      startActivity(mapIntent);
    });
  }
}

 

이처럼 인텐트를 사용하면, 외부의 지도앱으로 사용자를 보내 지도 서비스를 이용하게끔 할 수 있습니다.