4. Parse data(google place API) - waterglass2s/GraduationProject GitHub Wiki
Android Studio compileSdkVersion 29 / buildToolsVersion 29.0.3
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
- onMapReady
@Override
public void onMapReady(GoogleMap googleMap) {
//check permissions
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
}- 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);
}
});
}- 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();
}- 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.
- 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);