我有一个功能可以显示我定位附近的咖啡馆,并在 map 上将其标记为蓝色标记。是否可以在点击 mark 时获取有关 mark 标记的地点的信息?
点击标记后,我必须保存有关所选地点的信息。
我的代码: 下面是查找我本地化附近的咖啡馆的功能
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 的 documentation 使用 OnMarkerClickListener
来监听标记上的点击事件.
一旦您获得了通过现有附近搜索实现获得的地点的地点 ID,您就可以使用 Place Details获取这些地方信息的服务。例如:
// 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());
}
});
希望这对您有所帮助!
关于java - 如何获取有关标记显示的地点的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463668/