Android如何在谷歌地图中进行搜索而不使用谷歌地方API

时间:2014-07-23 11:08:27

标签: android api google-maps google-api

我使用Place API来搜索最近的地方使用Google提供的关键字(餐厅,ATM),但我想搜索" HeadShop" Google Place API 键功能无法使用。

我的问题是如何搜索" headshop"不使用场所API。

如果有人认同此问题,请帮助我谢谢

1 个答案:

答案 0 :(得分:0)

使用Google地理编码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));
         }

    }