我正在使用以下代码在 android 中设置地图视图并显示位置标记,效果很好。
此外,我正在尝试使用 maps.googleapis.com 和我的 api 密钥来检索 + 显示附近的 XML 附近位置数据,无论是使用 Toast 消息还是在调试器中
我知道我不应该在主线程上运行 HTTPrequest,但出于调试目的,我只是想确认我实际上是在检索所需的 JSON 数据
(它在浏览器中使用“ https://maps.googleapis.com/maps/api/place/search/json?location=39.9165171,116.450872&radius=50&sensor=false&key=MYKEY ”)例如
我在 Logcat 中遇到无法解决主机错误
public class MainActivity extends MapActivity implements LocationListener {
MyLocationOverlay myLocationOverlay;
public MapView mapView;
MapController mc;
OverlayItem overlayitem;
List<Overlay> mapOverlays;
HelloItemizedOverlay itemizedoverlay;
GeoPoint myLocationGeoPoint;
String result = "nothing";
private MockGpsProvider mMockGpsProviderTask = null;
/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
final String TAG = getClass().getSimpleName();
String googleAPIKey = "xxxxxxxxx";
String searchRadius = "50";
String baseUrl = "https://maps.googleapis.com/maps/api/place/search/json?";
String lat = String.valueOf(location.getLatitude());
String lon = String.valueOf(location.getLongitude());
String url = baseUrl + "location=" + lat + "," + lon + "&" +
"radius=" + searchRadius + "&" + "sensor=false" +
"&" + "key=" + googleAPIKey;
Log.v(TAG,url);
Toast.makeText(this, url, Toast.LENGTH_LONG).show();
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
Log.v(TAG,result);
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}