【问题标题】:Android How to Make Search in Google Map Without using google place APIAndroid 如何在不使用 google place API 的情况下在 Google Map 中进行搜索
【发布时间】:2014-07-23 11:08:27
【问题描述】:

我已经使用 Place API 来搜索最近的地方,使用 Google 提供的关键字(餐厅、ATM),但我想搜索 Google place API 中不可用的“HeadShop” 键。

我的问题是如何在不使用地点 API 的情况下搜索“headshop”。

如果有人发现这个问题,请帮助我,谢谢

【问题讨论】:

  • 如果某个第三方提供了它的 API,你可以使用它们,或者创建你自己的,或者让它完全由用户输入。

标签: android api google-maps google-api


【解决方案1】:

使用谷歌地理编码 API 编码您自己的地理编码器。

你可以开始here

使用Asynctask !!传递您的位置名称并从地理编码器获取 LatLng 值并将它们绘制在您的 Maps

这是我的代码

private class GeocoderTask extends AsyncTask<String, Void, LatLng> {

        @Override
        protected LatLng doInBackground(String... locationName) {
            // Creating an instance of Geocoder class
            Log.d(tag, "background");
            Geocoder geocoder = new Geocoder(getBaseContext(), Locale.ENGLISH);
            List<Address> addresses = null;
            String loc = locationName[0];
            LatLng position = null;

            try {
                // Getting a maximum of 3 Address that matches the input text
                addresses = geocoder.getFromLocationName(locationName[0], 3);
                Log.d("bump", "background try ");
                if (addresses != null && !addresses.isEmpty()) {
                    Address first_address = addresses.get(0);
                     position = new LatLng(first_address.getLatitude(),
                     first_address.getLongitude());
                      return position;

                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            if (position == null) {
                HttpGet httpGet = new HttpGet(
                        "http://maps.google.com/maps/api/geocode/json?address="
                                + loc + "&sensor=true");

                HttpClient client = new DefaultHttpClient();
                HttpResponse response;
                StringBuilder stringBuilder = new StringBuilder();

                try {
                    response = client.execute(httpGet);
                    HttpEntity entity = response.getEntity();
                    InputStream stream = entity.getContent();
                    int b;
                    while ((b = stream.read()) != -1) {
                        stringBuilder.append((char) b);
                    }
                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                }

                JSONObject jsonObject = new JSONObject();
                try {
                    // Log.d("MAPSAPI", stringBuilder.toString());

                    jsonObject = new JSONObject(stringBuilder.toString());
                    if (jsonObject.getString("status").equals("OK")) {
                        jsonObject = jsonObject.getJSONArray("results")
                                .getJSONObject(0);
                        jsonObject = jsonObject.getJSONObject("geometry");
                        jsonObject = jsonObject.getJSONObject("location");
                        String lat = jsonObject.getString("lat");
                        String lng = jsonObject.getString("lng");

                        // Log.d("MAPSAPI", "latlng " + lat + ", "
                        // + lng);

                        position = new LatLng(Double.valueOf(lat),
                                Double.valueOf(lng));
                    }

                } catch (JSONException e) {
                    Log.e(tag, e.getMessage(), e);
                }
            }
            return position;
        }

        @Override
        protected void onPostExecute(LatLng result) {
            Log.d("bump", "post");

            MarkerOptions markerOptions = new MarkerOptions();
              markerOptions.position(result); //


            googleMap.animateCamera(CameraUpdateFactory.newLatLng(result));
         }

    }

【讨论】:

  • 感谢 Rhth,此代码对于搜索位置很有用,但我想通过包含当前纬度和经度来搜索最近的总部。我可以在上面的网址中输入当前的经纬度吗?
  • 是的..因为它给出了 3 个结果检查 LatLng 与当前位置的最近距离,然后仅标记该点
  • Rhth 我把当前 loc 和 Long 放在 url 但它找到当前地址,我想“搜索”而不是寻找当前地址,想使用当前位置搜索 headshop。
  • 所以你总是想只搜索headshop??然后将核心位置名称改为“headshop”。“使用当前位置搜索”是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多