如何获得有关标记所显示位置的信息

问题描述 投票:1回答:1

我具有在我的本地化位置附近显示咖啡馆并将其在地图上标记为蓝色标记的功能。当我单击它时,是否可以获得有关带有标记标记的地方的信息?

单击标记后,我必须保存有关所选地点的信息。

我的代码:以下是根据我的本地化位置查找附近咖啡馆的功能

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);
        }
    }
}
java android google-maps
1个回答
0
投票

您可以根据Google的OnMarkerClickListener使用documentation来监听标记上的点击事件。

一旦有了通过现有的附近搜索实现获取的地点的地点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());
    }
});

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.