Android

ํ‚ค์›Œ๋“œ ์ž…๋ ฅํ•˜๋ฉด ๋‚ด ์ฃผ๋ณ€์— ์žˆ๋Š” ์žฅ์†Œ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ง€๋„ ๋„์›Œ์ฃผ๋Š” ์•ฑ(app) ๋งŒ๋“ค๊ธฐ (nearbysearch API ์‚ฌ์šฉ)

567Rabbit 2024. 6. 19. 17:11

 

https://developers.google.com/maps/documentation/places/web-service/search-nearby?hl=ko&_gl=1*tngnvs*_up*MQ..*_ga*ODgyOTU1NjIwLjE3MTg3NzQ1MjI.*_ga_NRWSTWS78N*MTcxODc3NDUyMi4xLjAuMTcxODc3NDUyMi4wLjAuMA..

 

์ฃผ๋ณ€ ์ง€์—ญ ๊ฒ€์ƒ‰  |  Places API  |  Google for Developers

์ด์ œ Places API (์‹ ๊ทœ)๊ฐ€ ์ถœ์‹œ๋˜๋ฉด์„œ ์ฐจ์„ธ๋Œ€ Places API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํŽ˜์ด์ง€๋Š” Cloud Translation API๋ฅผ ํ†ตํ•ด ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜๊ฒฌ ๋ณด๋‚ด๊ธฐ ์ฃผ๋ณ€ ์ง€์—ญ ๊ฒ€์ƒ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ

developers.google.com

 

 

 

 

์ €์žฅ ํ›„, ํ‚ค ๋ณต์‚ฌํ•˜๊ธฐ

 

 

 

 

ํ™˜๊ฒฝ์„ค์ •ํ•˜๊ธฐ

 

https://codebunny99.tistory.com/179

 

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค์—์„œ ๊ตฌ๊ธ€ ๋งต ์‚ฌ์šฉํ•˜๊ธฐ

๊ตฌ๊ธ€๋งต ์‚ฌ์šฉํ•˜๊ธฐhttps://developers.google.com/maps/documentation/places/web-service/search?hl=ko#nearby-search-and-text-search-responses ์žฅ์†Œ ๊ฒ€์ƒ‰  |  Places API  |  Google for Developers์ด์ œ Places API (์‹ ๊ทœ)๊ฐ€ ์ถœ์‹œ๋˜๋ฉด์„œ ์ฐจ์„ธ

codebunny99.tistory.com

 

 

 

Manifest์—์„œ ์ž‘์„ฑํ•ด์ฃผ๊ธฐ

<uses-permission android:name="android.permission.INTERNET"/>

 

 

 


๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

 

build.gradle.kts(:app) ์—์„œ, ๋งจ ์•„๋žซ์ค„ ์ž‘์„ฑ.

 

    implementation("com.squareup.retrofit2:retrofit:2.11.0")
    implementation("com.squareup.retrofit2:converter-gson:2.11.0")
    implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")

    implementation("com.github.bumptech.glide:glide:4.16.0")
    implementation("commons-io:commons-io:2.4")

 

 

 

 

 

 

 

 

config, NetworkClient ์ž‘์„ฑํ•˜๊ธฐ

 

config

package com.~.placeapp.config;

public class Config {
    public static final String DOMAIN = "https://maps.googleapis.com";

    public static final String SP_NAME = "places_app";

    public static final String PLACE_API_KEY = "์ž์‹ ์˜ API ํ‚ค ์ž…๋ ฅ";

}

 

 

NetworkClient

package com.~.placeapp.api;

import android.content.Context;

import com.~.placeapp.config.Config;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetworkClient {
    public static Retrofit retrofit;

    public static Retrofit getRetrofitClient(Context context){
        if(retrofit == null){
            // ํ†ต์‹  ๋กœ๊ทธ ํ™•์ธํ• ๋•Œ ํ•„์š”ํ•œ ์ฝ”๋“œ
            HttpLoggingInterceptor loggingInterceptor =
                    new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            // ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ๊ด€๋ จ ์ฝ”๋“œ
            OkHttpClient httpClient = new OkHttpClient.Builder()
                    .connectTimeout(1, TimeUnit.MINUTES)
                    .readTimeout(1, TimeUnit.MINUTES)
                    .writeTimeout(1, TimeUnit.MINUTES)
                    .addInterceptor(loggingInterceptor)
                    .build();
            // ๋„คํŠธ์›Œํฌ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ณ  ๋ฐ›๋Š”
            // ๋ ˆํŠธ๋กœํ• ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ด€๋ จ ์ฝ”๋“œ
            retrofit = new Retrofit.Builder()
                    .baseUrl(Config.DOMAIN)
                    .client(httpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

 


PlaceApi

package com.~.placeapp.api;

import com.~.placeapp.model.PlaceList;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface PlaceApi {

    //ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ ์žฅ์†Œ ๊ฐ€์ ธ์˜ค๋Š” API
    @GET("/maps/api/place/nearbysearch/json")
    Call<PlaceList> getPlaceList(@Query("language") String language,
                                 @Query("location") String location,
                                 @Query("radius") int radius,
                                 @Query("key") String key,
                                 @Query("keyword") String keyword);

}

 

 

 

 

activity_main ํ™”๋ฉด์ž‘์„ฑ

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/layoutTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="20dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/editKeyword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ..."
                android:inputType="text"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/imgSearch"
                android:layout_width="40dp"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/search_24dp" />

        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/layoutTop" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 


layout์— place_row.xml ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ์— ๋“ค์–ด๊ฐˆ ์นด๋“œ๋ทฐ ์ž‘์„ฑ

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="22sp" />

            <TextView
                android:id="@+id/txtVicinity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:text="TextView"
                android:textSize="22sp" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

model ํŒจํ‚ค์ง€ ์ž‘์„ฑ

 

Place ํด๋ž˜์Šค

package com.~.placeapp.model;

import com.google.gson.JsonArray;

public class Place implements Serializable {

    public String name;
    public String vicinity;
    public Geometry geometry;


    // ์ด๋„ˆ ํด๋ž˜์Šค inner class
    public class Geometry implements Serializable {

        public Location location;

        // ์ด๋„ˆ ํด๋ž˜์Šค inner class
        public class Location implements Serializable {
            public double lat;
            public double lng;
        }

    }

}

 


PlaceList

package com.~.placeapp.model;

import java.util.ArrayList;

public class PlaceList {

    public ArrayList<Place> results;
    
}

 

 

 

 

PlaceAdapter

 

package com.~.placeapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.~.placeapp.MapActivity;
import com.~.placeapp.R;
import com.~.placeapp.model.Place;

import java.util.ArrayList;

public class PlaceAdapter extends RecyclerView.Adapter<PlaceAdapter.ViewHolder> {

    Context context;
    ArrayList<Place> placeArrayList;

    public PlaceAdapter(Context context, ArrayList<Place> placeList) {
        this.context = context;
        this.placeArrayList = placeList; // ์—ฌ๊ธฐ์„œ placeArrayList์— ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.place_row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Place place = placeArrayList.get(position);

        if (place.name == null) {
            holder.txtName.setText("์ƒ์ ๋ช… ์—†์Œ");
        } else {
            holder.txtName.setText(place.name);
        }

        if (place.vicinity == null) {
            holder.txtVicinity.setText("์ฃผ์†Œ ์—†์Œ");
        } else {
            holder.txtVicinity.setText(place.vicinity);
        }
    }

    @Override
    public int getItemCount() {
        return placeArrayList != null ? placeArrayList.size() : 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView txtName;
        TextView txtVicinity;
        CardView cardView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtName = itemView.findViewById(R.id.txtName);
            txtVicinity = itemView.findViewById(R.id.txtVicinity);
            cardView = itemView.findViewById(R.id.cardView);

            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, MapActivity.class);
                    int index = getAdapterPosition();
                    if (index != RecyclerView.NO_POSITION) {
                        Place place = placeArrayList.get(index);
                        intent.putExtra("place", place);
                        context.startActivity(intent);
                    }
                }
            });
        }
    }
}

 

 

 

 

MainActivity

package com.~.placeapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.~.placeapp.adapter.PlaceAdapter;
import com.~.placeapp.api.NetworkClient;
import com.~.placeapp.api.PlaceApi;
import com.~.placeapp.config.Config;
import com.~.placeapp.model.Place;
import com.~.placeapp.model.PlaceList;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class MainActivity extends AppCompatActivity {

    LocationListener locationListener;
    LocationManager locationManager;

    EditText editKeyword;
    ImageView imgSearch;
    ProgressBar progressBar;
    RecyclerView recyclerView;
    ArrayList<Place> placeArrayList = new ArrayList<>();
    PlaceAdapter adapter;

    //ํ˜„์žฌ ๋‚˜์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์œ„๋„, ๊ฒฝ๋„ ๋ฉค๋ฒ„๋ณ€์ˆ˜
    double lat;
    double lng;

    String keyword;


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

        editKeyword = findViewById(R.id.editKeyword);
        imgSearch = findViewById(R.id.imgSearch);
        progressBar = findViewById(R.id.progressBar);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));


        // ํ˜„์žฌ ํฐ์˜ ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ ์ž‘์„ฑ.
        // 1. ๋กœ์ผ€์ด์…˜ ๋งค๋‹ˆ์ €๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        // 2. ์œ„์น˜๊ฐ€ ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค ์œ„์น˜์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ ์ž‘์„ฑ
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {
                lat = location.getLatitude();
                lng = location.getLongitude();

                Log.i("PLACES MAIN", "์œ„๋„ : " + lat + ", ๊ฒฝ๋„ : " + lng);
            }
        };

        // 3. ๋กœ์ผ€์ด์…˜ ๋งค๋‹ˆ์ €์—, ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค.
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION},
                    100);
            return;
        }

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000,
                -1,
                locationListener);  //minTimeMs์ดˆ๋งˆ๋‹ค ํ•œ๋ฒˆ์”ฉ ๋˜๋Š” minDistanceM๋ฏธํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ•œ๋ฒˆ์”ฉ ์‹คํ–‰ ์‹œํ‚จ๋‹ค.

        progressBar.setVisibility(View.GONE);

        imgSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keyword = editKeyword.getText().toString().trim();

                if(keyword.isEmpty()){
                    return;
                }

                getNetworkData();

            }
        });

    }

    private void getNetworkData() {

        progressBar.setVisibility(View.VISIBLE);

        placeArrayList.clear();

        //๋„คํŠธ์›Œํฌ ํ˜ธ์ถœ
        Retrofit retrofit = NetworkClient.getRetrofitClient(MainActivity.this);

        PlaceApi api = retrofit.create(PlaceApi.class);

        Call<PlaceList> call = api.getPlaceList("ko", lat+","+lng, 2000, Config.PLACE_API_KEY, keyword);

        call.enqueue(new Callback<PlaceList>() {
            @Override
            public void onResponse(Call<PlaceList> call, Response<PlaceList> response) {
                progressBar.setVisibility(View.GONE);

                if(response.isSuccessful()){
                    PlaceList placeList = response.body();
                    placeArrayList.addAll(placeList.results);

                    adapter = new PlaceAdapter(MainActivity.this, placeArrayList);
                    recyclerView.setAdapter(adapter);

                } else {

                }
            }

            @Override
            public void onFailure(Call<PlaceList> call, Throwable throwable) {
                progressBar.setVisibility(View.GONE);
            }
        });

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 100) { // ์š”์ฒญ ์ฝ”๋“œ๊ฐ€ 100์ผ ๋•Œ๋งŒ ์ฒ˜๋ฆฌ
            // grantResults ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•˜์—ฌ IndexOutOfBoundsException ๋ฐฉ์ง€
            if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                // ์œ„์น˜ ๊ถŒํ•œ์ด ์Šน์ธ๋˜์—ˆ์„ ๋•Œ
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                    if (locationManager != null) { // locationManager๊ฐ€ null์ด ์•„๋‹Œ์ง€ ํ™•์ธ
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                3000,
                                -1,
                                locationListener); // minTimeMs์ดˆ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ๋˜๋Š” minDistanceM๋ฏธํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
                    }
                }
            }
        }
    }

}

 

 

 

 

 

์นด๋“œ๋ทฐ ๋ˆ„๋ฅด๋ฉด ์ง€๋„ ๋œจ๋Š” MapActivity ๋งŒ๋“ค๊ธฐ

 

activity_map.xml : ์ง€๋„๋กœ ํ•œ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

 

 

MapActivity.java

package com.~.placeapp;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.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;
import com.yujinoh.placeapp.model.Place;


public class MapActivity extends AppCompatActivity {

    Place place;

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

        place = (Place) getIntent().getSerializableExtra("place");

        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull GoogleMap googleMap) {

                // ์œ„์—์„œ ๋ฐ›์•„์˜จ place ๊ฐ์ฒด์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์œ„๋„, ๊ฒฝ๋„ ๊บผ๋‚ด์„œ
                LatLng latLng = new LatLng(place.geometry.location.lat, place.geometry.location.lng);

                // 1. ์ง€๋„์˜ ์œ„์น˜๋ฅผ ์ด ์œ„๋„, ๊ฒฝ๋„๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ํ•ด์„œ ์ด๋™์‹œํ‚จ๋‹ค.
                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));

                // 2. ๋งˆ์ปค๋กœ ํ‘œ์‹œํ•œ๋‹ค.
                MarkerOptions markerOptions = new MarkerOptions();
                if(place.name == null){
                    markerOptions.position(latLng).title("์ƒ์ ๋ช… ์—†์Œ");
                }else{
                    markerOptions.position(latLng).title(place.name);
                }
                googleMap.addMarker(markerOptions);

            }
        });

    }
}

 

 

 

ํด๋ฆญํ•˜๋ฉด, 

 

 

 

 

 

 

 

์•ก์…˜๋ฐ”์˜ ์ง€๋„ ์•„์ด์ฝ˜ ํด๋ฆญํ•˜๋ฉด, ๊ฒ€์ƒ‰ํ•œ ๊ณณ์„ ๋ชจ๋‘ ์ง€๋„๋กœ ๋งˆ์ปค ํ‘œ์‹œํ•˜๊ฒŒ ํ•˜๊ธฐ

 

 

 

๋ฉ”๋‰ด๋ฅผ ๋งŒ๋“ ๋‹ค.

 

 

 

 

 

 

Activity ๋งŒ๋“ค๊ธฐ : PlaceActivity๋กœ ํ•˜์˜€๋‹ค.

 

activity_place.xml ํ™”๋ฉด ์ฝ”๋“œ : ์ง€๋„๋กœ ํ•œ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

 

 

 

PlaceActivity ์ž‘์„ฑํ•˜๊ธฐ

package com.~.placeapp;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.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;
import com.yujinoh.placeapp.model.Place;

import java.util.ArrayList;


public class PlaceActivity extends AppCompatActivity {

    Place place;
    ArrayList<Place> placeArrayList;
    double lat;
    double lng;

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

        // ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ค๊ธฐ
        placeArrayList = (ArrayList<Place>) getIntent().getSerializableExtra("placeArrayList");
        lat = getIntent().getDoubleExtra("lat", 0);
        lng = getIntent().getDoubleExtra("lng", 0);

        
        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull GoogleMap googleMap) {

                // 1. ์œ„์—์„œ ๋ฐ›์•„์˜จ place ๊ฐ์ฒด์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์œ„๋„, ๊ฒฝ๋„ ๊บผ๋‚ด์„œ
                LatLng myLocation = new LatLng(lat, lng);
                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 17));

                // 2. ์œ„์—์„œ ๋ฐ›์€ ์–ด๋ ˆ์ด๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด์žˆ๋Š” ํ”Œ๋ ˆ์ด์Šค๋ฅผ ๋ฐ˜๋ณต๋ฌธํ•ด์„œ ๋งˆ์ปค๋กœ ๋งŒ๋“ ๋‹ค.
                
                for( Place place : placeArrayList ){
                    LatLng latLng = new LatLng(place.geometry.location.lat, place.geometry.location.lng);
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(latLng).title(place.name);
                    googleMap.addMarker(markerOptions);
                }
                
            }
        });
        
        
    }
}

 

 

 

 

 

 

 

 

 

GPS๊ฐ€ ์ค€๋น„๋˜์–ด์žˆ์ง€ ์•Š์•˜๋Š”๋ฐ, ์ง€๋„๋ฅผ ๋ˆ„๋ฅด๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ์˜ค๋ฅ˜๋ฅผ ์žก๊ธฐ์œ„ํ•ด MainActivity์—  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

 

 

 

(1) ๋ณ€์ˆ˜ ์„ค์ •

 

 

 

 

 

(2) ์•„์ง ์ค€๋น„๊ฐ€ ์•ˆ๋์„ ๋•Œ, ํŒ์—…์ฐฝ์„ ๋„์šด๋‹ค.

 

 

 

 

 

(3) ์ค€๋น„๊ฐ€ ๋˜์—ˆ์„ ๋•Œ์—๋Š” ์ž˜ ๋™์ž‘ํ•˜๋„๋ก true๋กœ ํ•œ๋‹ค