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);
});
}
}
이처럼 인텐트를 사용하면, 외부의 지도앱으로 사용자를 보내 지도 서비스를 이용하게끔 할 수 있습니다.
'안드로이드 자바' 카테고리의 다른 글
[Java][Android] commons-io 활용 예시 만들기 (0) | 2025.05.30 |
---|---|
[Java][Android] SharedPreferences 데이터 암호화 (0) | 2025.05.26 |
[Java][Android] LifecycleOwner 활용 예시 만들기 (1) | 2025.05.23 |
[Java][Android] 랜덤 Random 클래스의 다양한 활용법 (2) | 2025.05.20 |
[Java][Android] 화면 회전 onSaveInstanceState()로 데이터 지키기 (0) | 2025.05.19 |