如何在Android中使用Google Place API获取最近的位置

时间:2014-05-26 09:27:45

标签: android google-maps google-places-api

我正在实施find the nearest location(机场,酒店等)。我已经实现了一些代码,并在Google Place API上注册了我的应用程序。

我收到了一个错误。我已经将我的代码放在这里,并使用了我使用的API密钥的屏幕截图。

{

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    CameraPosition cameraPosition = new CameraPosition.Builder().target(
    new LatLng(latitude,longitude)).zoom(18).build();
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    marker = new MarkerOptions().position(new LatLng(latitude, longitude));
    googleMap.addMarker(marker.draggable(true));
    googleMap.setIndoorEnabled(true);
    StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/geocode/json?");
                        sb.append("location="+latitude+","+longitude);
                        sb.append("&radius=5000");
                        sb.append("&types="+"Mosque");
                        sb.append("&sensor=true");
                        sb.append("&key=apikey is shown in image below");
                        System.out.println("url :-" + sb.toString());
                        PlacesTask placesTask = new PlacesTask();                                   
                        placesTask.execute(sb.toString());
    }

    private class PlacesTask extends AsyncTask<String, Integer, String>{
                        String data = null;
        @Override
        protected String doInBackground(String... url) {
            try{
                data = downloadUrl(url[0]);
            }catch(Exception e){
                 Log.d("Background Task",e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result){            
            System.out.println("Data :- " + data);
        }
    }

    /** A method to download json data from url */
    private String downloadUrl(String strUrl) throws IOException{
        String data = "";
        InputStream iStream = null;
        HttpURLConnection urlConnection = null;
        try{
                 URL url = new URL(strUrl);                
                 urlConnection = (HttpURLConnection) url.openConnection();                
                 urlConnection.connect();                
                 iStream = urlConnection.getInputStream();
                 BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
                  StringBuffer sb  = new StringBuffer();
                  String line = "";
                  while( ( line = br.readLine())  != null){
                          sb.append(line);
                  }
                   data = sb.toString();
                   br.close();
          }catch(Exception e){
                  Log.d("Exception while downloading url", e.toString());
          }finally{
                  iStream.close();
                  urlConnection.disconnect();
          }
          return data;
     }     

API控制台

错误

  
    

{&#34; error_message&#34; :&#34;此API项目无权使用此API。&#34;,&#34;结果&#34; :[],&#34;状态&#34; :&#34; REQUEST_DENIED&#34;}

  

1 个答案:

答案 0 :(得分:0)

试试这个。

 LocationManager lm = (LocationManager) getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
    Location net_loc = null;
    lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

您可以从net_loc获取当前位置。