Google文字搜索网址无法在Android中运行

时间:2014-10-06 08:15:59

标签: android google-maps google-geocoder

在此我尝试点击URL bacground“response1”但响应1是打印空值,问题是什么。但“HttpRes”是打印也在url中添加地址。请回答最新的问题。

在我的代码中Json没有在logcat中显示当使用textsearch url时我的代码中出现了什么问题............................. .................

10-06 09:50:56.391:I / System.out(19964):GeocoderTask latlnglat / lng:(19.0759837,72.8776559)10-06 09:50:56.395:I / System.out(19964):AddressText ===>>>孟买,马哈拉施特拉邦,印度10-06 09:50:56.399:I / System.out(19964):HttpRes响应---->>>> {{3} },Maharashtra,India& key = AIzaSyAoXVbnsD_AV8ejPliFjT3vIEtEXsv1lPc 10-06 09:50:56.399:I / System.out(19964):Addresstext response ---->>>> null 10-06 09:50:56.399 :I / System.out(19964):Geocoder doInBackground响应为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);


    }
}

2 个答案:

答案 0 :(得分:0)

您的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 path位置的first pathhome user directory中的second path。对于windows 7,它就像C:\Users\username\.android\debug.keystore

使用上面的命令,您将获得所有密钥。

The google API key is here

之后

据我所知,通过调用GoogleMap对象的moveCamera()方法将地图移动到某个点:

 GoogleMap map = ((MapFragment) getFragmentManager()
            .findFragmentById(R.id.map)).getMap();

 LatLng sydney = new LatLng(-33.867, 151.206);

 map.setMyLocationEnabled(true);
 map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

取自this示例。

如您所见,您需要使用纬度和经度坐标来创建LatLng对象。我相信您可以使用谷歌搜索来获取这些坐标,使用http请求。例如:

https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere

取自here

应该为您返回JSON数据。

有关自动填充功能,请参阅this链接

编辑: Wops,地点搜索会返回xml,尝试使用... json?query = ...而不是

答案 1 :(得分:0)

在我的代码中Json在使用textsearch url时没有在logcat中显示 我上面的代码中有什么问题............................................ ..

10-06 09:50:56.391:I / System.out(19964):GeocoderTask latlnglat / lng:(19.0759837,72.8776559) 10-06 09:50:56.395:I / System.out(19964):AddressText ===&gt;&gt;&gt;孟买,马哈拉施特拉邦,印度 10-06 09:50:56.399:I / System.out(19964):HttpRes响应----&gt;&gt;&gt;&gt; https://maps.googleapis.com/maps/api/place/textsearch/json?query=Mumbai,马哈拉施特拉邦,印度&amp; key = AIzaSyAoXVbnsD_AV8ejPliFjT3vIEtEXsv1lPc 10-06 09:50:56.399:I / System.out(19964):地址文本响应----&gt;&gt;&gt;&gt; null 10-06 09:50:56.399:I / System.out(19964):Geocoder doInBackground响应为null