我有一个功能,可以显示我本地化附近的咖啡馆,并在地图上将其标记为蓝色标记。当我点击它时,是否可以获得有关标记标记的地点的信息?
单击标记后,我必须保存有关所选地点的信息。
我的代码: 以下是在我的本地化附近查找咖啡馆的功能
public void findCafe(View view){
StringBuilder stringBuilder = new StringBuilder( "https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
String locationStr = clatlng.latitude +","+clatlng.longitude ;
stringBuilder.append( "location="+ locationStr);
stringBuilder.append( "&radius=").append( PROXIMITY_RADIUS );
stringBuilder.append( "&type="+"cafe");
stringBuilder.append("&sensor=true");
stringBuilder.append( "&key="+getResources().getString( R.string.new_key ));
String url = stringBuilder.toString();
JsonObjectRequest request = new JsonObjectRequest(url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject result) {
Log.i(TAG, "onResponse: Result= " + result.toString());
try {
parseLocationResult(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse: Error= " + error);
Log.e(TAG, "onErrorResponse: Error= " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(request);
}
public class AppController extends Application {
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
根据Google的文档,您可以使用OnMarkerClickListener
来侦听标记上的点击事件。
获得通过现有附近搜索实现获得的地点的地点 ID 后,您可以使用地点详细信息服务获取这些地点的信息。 例如:
// Define a Place ID.
String placeId = "INSERT_PLACE_ID_HERE";
// Specify the fields to return.
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Construct a request object, passing the place ID and fields array.
FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
Place place = response.getPlace();
Log.i(TAG, "Place found: " + place.getName());
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
int statusCode = apiException.getStatusCode();
// Handle error with given status code.
Log.e(TAG, "Place not found: " + exception.getMessage());
}
});
希望这有帮助!