Reputation: 73
I am trying to apply the new library that android added after 29,January 2019. All the old apps are using maps and places sdk are their methods are deprecated. I have tried to read all the new docs but it seems that the new library doesn't support nearby search but only current location. Correct me if I am wrong, please.
Upvotes: 3
Views: 7545
Reputation: 5691
The below code would be an example you can use for guidance of how you can call the Nearby Search service from the Java client within your Android app:
MapsActivity.java
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.fragment.app.FragmentActivity;
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.google.maps.model.PlacesSearchResult;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private Context mContext;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
PlacesSearchResult[] placesSearchResults = new NearbySearch().run().results;
Log.e("response1Tag", placesSearchResults[0].toString());
Log.e("response2Tag", placesSearchResults[1].toString());
double lat1 = placesSearchResults[0].geometry.location.lat;
double lng1 = placesSearchResults[0].geometry.location.lng;
double lat2 = placesSearchResults[1].geometry.location.lat;
double lng2 = placesSearchResults[1].geometry.location.lng;
mMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1)));
mMap.addMarker(new MarkerOptions().position(new LatLng(lat2, lng2)));
mMap.setMinZoomPreference(14.0f);
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat1, lng1)));
}
}
NearbySearch.java
import com.google.maps.GeoApiContext;
import com.google.maps.PlacesApi;
import com.google.maps.errors.ApiException;
import com.google.maps.model.PlaceType;
import com.google.maps.model.PlacesSearchResponse;
import com.google.maps.model.RankBy;
import com.google.maps.model.LatLng;
import java.io.IOException;
public class NearbySearch {
public PlacesSearchResponse run(){
PlacesSearchResponse request = new PlacesSearchResponse();
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("MY_KEY")
.build();
LatLng location = new LatLng(-33.8670522, 151.1957362);
try {
request = PlacesApi.nearbySearchQuery(context, location)
.radius(5000)
.rankby(RankBy.PROMINENCE)
.keyword("cruise")
.language("en")
.type(PlaceType.RESTAURANT)
.await();
} catch (ApiException | IOException | InterruptedException e) {
e.printStackTrace();
} finally {
return request;
}
}
}
In build.gradle add:
implementation 'com.google.maps:google-maps-services:0.11.0'
Screenshot:
Note that the API key you'll use to call the API should be secured properly, e.g. you may need to set up a proxy server. Take a look at these techniques.
Hope this helps!
Upvotes: 2