Google Places API:如何从纬度和经度中获取照片并放置ID? [英] Google Places API: How to get photos and place id from latitude and longitude?

查看:162
本文介绍了Google Places API:如何从纬度和经度中获取照片并放置ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得照片和Google Place所需的place_id 详细信息我当前位置的API。

I want to get photos and place_id required by Google Place Details API of my current location.

附近搜索不会返回我确切位置的地方。 (android位置服务返回的当前lat / lng)。

Search nearby does not return places in my exact location. (current lat/lng returned by android location service).

雷达搜索需要一个关键字。请建议。

Radar search requires a keyword. Please suggest.

推荐答案

根据 Google商家搜索文档您需要提供的三件事是KEY,LOCATION和RADIUS。我删除了一堆不必要的代码,但这是我做类似的事情。

Per the Google Place Search documentation the three things you need to provide are the KEY, LOCATION and RADIUS. I cut out a bunch of unnecessary code, but here's how I did something similar.

1)获取当前位置

private void initializeMapLocation() {
    LocationManager locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);

    Location lastLocation = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (lastLocation != null) {
        setUserLocation(lastLocation);
    }
}

private void setUserLocation(Location location) {
    LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
    mMap.animateCamera(CameraUpdateFactory.newLatLng(currentLatLng));
}

2)构建搜索网址。你可以通过附加它们来添加像关键字这样的额外参数,但在这种特殊情况下听起来并不像你想要的那样。

2) Build your search URL. You can add extra parameters like a keyword if you want by appending them on, but it doesn't sound like you want that in this particular case.

private void buildAndInitiateSearchTask(String searchType) {
    Projection mProjection = mMap.getProjection();
    LatLng mProjectionCenter = mProjection.getVisibleRegion().latLngBounds
        .getCenter();

    searchURL.append("https://maps.googleapis.com/maps/api/place/nearbysearch/");
    searchURL.append("json?");
    searchURL.append("location=" + mProjectionCenter.latitude + "," + mProjectionCenter.longitude);
    searchURL.append("&radius=" + calculateProjectionRadiusInMeters(mProjection));
    searchURL.append("&key=YOUR_KEY_HERE");

    new PlaceSearchAPITask().execute(searchURL.toString());
}

private double calculateProjectionRadiusInMeters(Projection projection) {

    LatLng farLeft = projection.getVisibleRegion().farLeft;
    LatLng nearRight = projection.getVisibleRegion().nearRight;

    Location farLeftLocation = new Location("Point A");
    farLeftLocation.setLatitude(farLeft.latitude);
    farLeftLocation.setLongitude(farLeft.longitude);

    Location nearRightLocation = new Location("Point B");
    nearRightLocation.setLatitude(nearRight.latitude);
    nearRightLocation.setLongitude(nearRight.longitude);

    return farLeftLocation.distanceTo(nearRightLocation) / 2 ;
}

3)发送您的请求并将结果显示为AsyncTask

3) Send your request and display your results as an AsyncTask

private class PlaceSearchAPITask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... placesURL) {
        StringBuilder placesBuilder = new StringBuilder();

        for (String placeSearchURL : placesURL) {
            HttpClient placesClient = createHttpClient();
            try {
                HttpGet placesGet = new HttpGet(placeSearchURL);
                HttpResponse placesResponse = placesClient
                        .execute(placesGet);
                StatusLine placeSearchStatus = placesResponse
                        .getStatusLine();
                if (placeSearchStatus.getStatusCode() == 200) {
                    HttpEntity placesEntity = placesResponse
                            .getEntity();
                    InputStream placesContent = placesEntity
                            .getContent();
                    InputStreamReader placesInput = new InputStreamReader(
                            placesContent);
                    BufferedReader placesReader = new BufferedReader(
                            placesInput);
                    String lineIn;
                    while ((lineIn = placesReader.readLine()) != null) {
                        placesBuilder.append(lineIn);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return placesBuilder.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            JSONObject resultObject = new JSONObject(result);

            // This is my custom object to hold the pieces of the JSONResult that I want. You would need something else for your particular problem.
            mapData = new MapDataSource(resultObject.optJSONArray("results"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (mapData != null) {
            // TODO - This is where you would add your markers and whatnot.
        } 
    }
}

这篇关于Google Places API:如何从纬度和经度中获取照片并放置ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆