4. Parse data(google place API) - waterglass2s/GraduationProject GitHub Wiki

Development Environment

Android Studio compileSdkVersion 29 / buildToolsVersion 29.0.3

Library version

gms:play-services-maps - 17.0.0 gms:play-services-location - 17.0.0 gms:play-services-places - 17.0.0 libraries.places:places - 2.4.0

How to parse Data (google map API)

Use map and draw markers when we get information

  1. onMapReady
 @Override
    public void onMapReady(GoogleMap googleMap) {
//check permissions 

        mMap.getUiSettings().setMyLocationButtonEnabled(true);
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
    }
  1. Get current location and get markers
LocationCallback locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);

            List<Location> locationList = locationResult.getLocations();

            if (locationList.size() > 0) {
                location = locationList.get(locationList.size() - 1);
                //location = locationList.get(0);

                currentPosition
                        = new LatLng(location.getLatitude(), location.getLongitude());


                String markerTitle = getCurrentAddress(currentPosition);
                String markerSnippet = "μœ„λ„:" + String.valueOf(location.getLatitude())
                        + " 경도:" + String.valueOf(location.getLongitude());

                Log.d(TAG, "onLocationResult : " + markerSnippet);


                //ν˜„μž¬ μœ„μΉ˜μ— 마컀 μƒμ„±ν•˜κ³  이동
                setCurrentLocation(location, markerTitle, markerSnippet);

                mCurrentLocation = location;
            }


        }

    };
@Override
    public void onPlacesSuccess(List<Place> places) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                for (noman.googleplaces.Place place : places) {

                    LatLng latLng
                            = new LatLng(place.getLatitude()
                            , place.getLongitude());

                    String markerSnippet = getCurrentAddress(latLng);

                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(latLng);
                    markerOptions.title(place.getName());
                    markerOptions.snippet(markerSnippet);
                    Marker item = mMap.addMarker(markerOptions);
                    previous_marker.add(item);

                }

                //쀑볡 마컀 제거
                HashSet<Marker> hashSet = new HashSet<Marker>();
                hashSet.addAll(previous_marker);
                previous_marker.clear();
                previous_marker.addAll(hashSet);

            }
        });
    }
  1. Show information on the map
if(place_value.equalsIgnoreCase("restaurant")){
            new NRPlaces.Builder()
                    .listener(MapMainActivity.this)
                    .key((your)key)
                    .latlng(location.latitude, location.longitude)//ν˜„μž¬ μœ„μΉ˜
                    .radius(Integer.parseInt(distance_value)) //500 λ―Έν„° λ‚΄μ—μ„œ 검색
                    .type(PlaceType.RESTAURANT) //μŒμ‹μ 
                    .build()
                    .execute();
            Toast.makeText(getApplicationContext(), "Success to matching places", Toast.LENGTH_SHORT).show();
        }

Get information from google place API web.ver

  1. First define view
String uStr = "(your_json_link)?location=" + lat + "," + lon
                    + "&radius=" + radius + "&types=" + type + "&key=(your_key)";

            URL url = new URL(uStr);

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));

            String line;
            while((line = in.readLine()) != null)
            {
                mResponseBuilder.append(line);
            }
            in.close();

We use web version of Google place API, because Google place API doesn't provide many information.
So, we have to get json file and parse it.

  1. Parse data
JSONArray jsonArray;
            JSONObject jsonObject;

            jsonObject = new JSONObject(mResponseBuilder.toString());
            jsonArray = jsonObject.getJSONArray("results");

            for(int i = 0; i < jsonArray.length(); i++)
            {
                JSONObject result = jsonArray.getJSONObject(i);

                //이름 μ–»κΈ°
                String store_name = result.getString("name");
                Log.d("check", "name : " + store_name);
⚠️ **GitHub.com Fallback** ⚠️