我已经使用地方API为搜索最近的地方使用关键字提供的谷歌喜欢(餐厅,ATM),但我想搜索"HeadShop"这是不可用的谷歌地方API keywork。
我的问题是如何在不使用位置API的情况下搜索"headshop"。
如果有人发现这个问题请帮助我谢谢
使用google geocoding API编写您自己的地理编码器。
你可以从这里开始
使用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));
}
}