谷歌文本搜索URL在android中不起作用
在本例中,我尝试点击一个URL bacground response1,但response1是print null值,这是什么问题。但HttpRes也可以在url中添加地址。请回答什么问题谷歌文本搜索URL在android中不起作用,android,google-maps,google-geocoder,Android,Google Maps,Google Geocoder,在本例中,我尝试点击一个URL bacground response1,但response1是print null值,这是什么问题。但HttpRes也可以在url中添加地址。请回答什么问题 在我的代码中,当使用textsearch url时,Json未在logcat中显示在我的上述代码中有什么问题 10-06 09:50:56.391:I/System.out19964:GeocoderTask latlnglat/lng:19.0759837,72.8776559 10-06 09:50:56
在我的代码中,当使用textsearch url时,Json未在logcat中显示在我的上述代码中有什么问题
10-06 09:50:56.391:I/System.out19964:GeocoderTask latlnglat/lng:19.0759837,72.8776559 10-06 09:50:56.395:I/System.out19964:AddressText===>>>印度马哈拉施特拉邦孟买10-06 09:50:56.399:I/System.out19964:HttpRes响应-->>>>,马哈拉施特拉邦,India&key=AIZASYAOXVBNSDAV8EJPLIFJT3VIETEXV1LPC 10-06 09:50:56.399:I/System.out19964:Addresstext response-->>>>null 10-06 09:50:56.399:I/System.out19964:Geocoder doInBackground response null
private class GeocoderTask extends AsyncTask<String, Void, String>{
String location,response1= null;
String addressText;
/**********************************************************/
public GeocoderTask(String location) {
this.location=location;
}
@Override
protected void onPreExecute() {
myServiceToHttp= new MyServiceToHttp();
List<Address> addresses = null;
Geocoder geocoder = new Geocoder(getBaseContext());
/**********************************************************/
try {
addresses = geocoder.getFromLocationName(location,1000 );
} catch (IOException e) {
e.printStackTrace();
}
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
googleMap.clear();
}
else {
if(addresses.size()>0)
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
lat = address.getLatitude();
lng = address.getLongitude();
addressText = String.format("%s, %s",address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",address.getCountryName());
System.out.println(" GeocoderTask latlng" +latLng);
Toast.makeText(getApplicationContext(), "latlng" +String.valueOf(latLng), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Geolat == "+lat+ "Geolng == " +lng, Toast.LENGTH_SHORT).show();
System.out.println("AddressText===>>>" +addressText);
Toast.makeText(getApplicationContext(), "addressText"+addressText, Toast.LENGTH_SHORT).show();
marker = new MarkerOptions();
marker.position(latLng);
marker.title(addressText);
googleMap.addMarker(marker);
// Locate the first location
if(i==0){
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng) // Sets the center of the map to Mountain View
.zoom(10) // Sets the zoom
.bearing(40) // Sets the orientation of the camera to east
.tilt(5) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
super.onPreExecute();
}
@Override
protected String doInBackground(String... locationName) {
response1 = myServiceToHttp.makeServiceCall("https://maps.googleapis.com/maps/api/place/textsearch/json?query="+addressText+"&key=YOUR SERVER KEY",MyServiceToHttp.GET);
String HttpRes = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="+addressText+"&key=YOUR SERVER KEY";
System.out.println("HttpRes response---->>>>" +HttpRes);
System.out.println("Addresstext response---->>>>" +response1);
if (response1!=null) {
try {
JSONObject jsonObject=new JSONObject(response1);
System.out.println("Json==>>>>"+jsonObject);
JSONArray array=jsonObject.getJSONArray("results");
System.out.println("JsonArray==>>>"+array);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
else
{
System.out.println("Geocoder doInBackground response null");
}
return response1;
}
@Override
protected void onPostExecute(String addresses) {
Toast.makeText(getApplicationContext(), addresses, Toast.LENGTH_SHORT).show();
super.onPostExecute(addresses);
}
}
你的API密钥在哪里?
试试这个
C:\Program Files\Java\jdk1.7.0_04\bin>keytool.exe -V -list -alias androiddebugkey -keystore "C:\Documents and Settings\IBM\.android\debug.keystore" -storepass android -keypass android
考虑第一个路径中的jdk bin路径和第二个路径中的主用户目录。对于windows 7,它类似于ec:\Users\username\.android\debug.keystore
使用上述命令,您将获得所有密钥
然后
在我看来,将地图移动到某个点是通过调用GoogleMap对象的moveCamera方法完成的:
从例子中可以看出
如您所见,创建LatLng对象需要纬度和经度坐标。我相信你可以使用谷歌的地点搜索,通过http请求获得这些坐标。例如:
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere
取自
这将为您返回JSON数据
有关自动完成,请参见链接
编辑:Wops,位置搜索将返回xml,请尝试使用…json?query=。。。相反,在我的代码中,当使用textsearch url时,Json不会显示在logcat中 我的上述代码中有什么问题
10-06 09:50:56.391:I/System.out19964:GeocoderTask latlnglat/lng:19.0759837,72.8776559 10-06 09:50:56.395:I/System.out19964:AddressText====>>>印度马哈拉施特拉邦孟买 10-06 09:50:56.399:I/System.out19964:HttpRes响应-->>>>>,印度马哈拉施特拉邦&key=AIzaSyAoXVbnsD_AV8ejPliFjT3vIEtEXsv1lPc 10-06 09:50:56.399:I/System.out19964:Addresstext响应-->>>>空
10-06 09:50:56.399:I/System.out19964:Geocoder doInBackground response null10-06 09:50:56.391:I/System.out19964:GeocoderTask latlnglat/lng:19.0759837,72.8776559 10-06 09:50:56.395:I/System.out19964:AddressText===>>印度马哈拉施特拉邦孟买10-06:50:56.399:I/System.out19964:HTPRES response-->,马哈拉施特拉邦,India&key=AIZASYAOXVBNSDAV8EJPLIFJT3VIETEXV1LPC 10-06 09:50:56.399:I/System.out19964:Addresstext response-->>>>null 10-06 09:50:56.399:I/System.out19964:Geocoder doInBackground response null
private class GeocoderTask extends AsyncTask<String, Void, String>{
String location,response1= null;
String addressText;
/**********************************************************/
public GeocoderTask(String location) {
this.location=location;
}
@Override
protected void onPreExecute() {
myServiceToHttp= new MyServiceToHttp();
List<Address> addresses = null;
Geocoder geocoder = new Geocoder(getBaseContext());
/**********************************************************/
try {
addresses = geocoder.getFromLocationName(location,1000 );
} catch (IOException e) {
e.printStackTrace();
}
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
googleMap.clear();
}
else {
if(addresses.size()>0)
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
lat = address.getLatitude();
lng = address.getLongitude();
addressText = String.format("%s, %s",address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",address.getCountryName());
System.out.println(" GeocoderTask latlng" +latLng);
Toast.makeText(getApplicationContext(), "latlng" +String.valueOf(latLng), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Geolat == "+lat+ "Geolng == " +lng, Toast.LENGTH_SHORT).show();
System.out.println("AddressText===>>>" +addressText);
Toast.makeText(getApplicationContext(), "addressText"+addressText, Toast.LENGTH_SHORT).show();
marker = new MarkerOptions();
marker.position(latLng);
marker.title(addressText);
googleMap.addMarker(marker);
// Locate the first location
if(i==0){
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng) // Sets the center of the map to Mountain View
.zoom(10) // Sets the zoom
.bearing(40) // Sets the orientation of the camera to east
.tilt(5) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
super.onPreExecute();
}
@Override
protected String doInBackground(String... locationName) {
response1 = myServiceToHttp.makeServiceCall("https://maps.googleapis.com/maps/api/place/textsearch/json?query="+addressText+"&key=YOUR SERVER KEY",MyServiceToHttp.GET);
String HttpRes = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="+addressText+"&key=YOUR SERVER KEY";
System.out.println("HttpRes response---->>>>" +HttpRes);
System.out.println("Addresstext response---->>>>" +response1);
if (response1!=null) {
try {
JSONObject jsonObject=new JSONObject(response1);
System.out.println("Json==>>>>"+jsonObject);
JSONArray array=jsonObject.getJSONArray("results");
System.out.println("JsonArray==>>>"+array);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
else
{
System.out.println("Geocoder doInBackground response null");
}
return response1;
}
@Override
protected void onPostExecute(String addresses) {
Toast.makeText(getApplicationContext(), addresses, Toast.LENGTH_SHORT).show();
super.onPostExecute(addresses);
}
}