關鍵在送出此API

 https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=座標&radius=搜尋範圍&types=種類&sensor=true&key=server api key

 

server api key(=金鑰) 要申請

官方網站: https://developers.google.com/places/web-service/

建立好的結果:

 

【搜尋澳洲雪梨達令港附近的餐廳,並依據距離排列結果】

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&types=food&key= server api key

【搜尋中山附近的餐廳,範圍是500公尺內】

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=25.0540279,121.5199219&radius=500&types=food&key= server api key

【搜尋中山各種資訊,範圍是500公尺內】

https://maps.googleapis.com/maps/api/place/nearbysearch/json?key= server api key &radius=500&location=25.0540279,121.5199219

就能得到json(擷取部分)

 

 

其他支援的類型 以後如果是要找醫院 type就要換 可以參考這網站:https://developers.google.com/places/supported_types#table1

下圖是網站內容結取部分

 

重點來了!Android 要如何處理這json?並顯示在自己寫的app?

AsyncTask 是必然使用,因為要連線,一定要用異步執行緒

HttpClient 是必然使用,因為要輸入網址(上面一直提到的https://maps.googleapis.com/maps/api/....以下省略)

 

先不做mark部分,先測試到底有沒有取得json在往下做

package com.example.user.exgooglemap;



import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;



import com.androidquery.callback.AjaxStatus;

import com.google.android.gms.maps.CameraUpdate;

import com.google.android.gms.maps.CameraUpdateFactory;

import com.google.android.gms.maps.GoogleMap;

import com.google.android.gms.maps.MapFragment;

import com.google.android.gms.maps.model.LatLng;



import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.BasicHttpParams;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;



import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;





public class MainActivity extends Activity {

  
    GPSTracker gps;

    String result = null;

    @Override

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);



        // 宣告使用 GPSTracker

        gps = new GPSTracker(this);



        if(gps.canGetLocation()){

            //取得經緯度

            double latitude = gps.getLatitude();

            double longitude = gps.getLongitude();



            //設定API

           
          String ListApi = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key= server api key &radius=500&location=25.0540279,121.5199219";

           new HttpAsyncTask().execute(ListApi);



        }else{

            System.out.println("===urlNO===");

            Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();

        }





    }

    private class HttpAsyncTask extends AsyncTask<String, Void, String> {

        @Override

        protected String doInBackground(String... urls) {

            return GET(urls[0]);

        }

        // onPostExecute displays the results of the AsyncTask.

        @Override

        protected void onPostExecute(String result) {

            try {





                JSONObject jObject = new JSONObject(result);

                for(int i = 0;i<jObject.getJSONArray("results").length();i++){

                    Log.i("results name", jObject.getJSONArray("results").getJSONObject(i).getString("name"));

                }



            } catch (JSONException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }



        private  String GET(String url){

            InputStream inputStream = null;

            String result = "";

            try {



                // create HttpClient

                HttpClient httpclient = new DefaultHttpClient();



                // make GET request to the given URL

                HttpResponse httpResponse = httpclient.execute(new HttpGet(url));



                // receive response as inputStream

                inputStream = httpResponse.getEntity().getContent();



                // convert inputstream to string

                if(inputStream != null)

                    result = convertInputStreamToString(inputStream);

                else

                    result = "Did not work!";



            } catch (Exception e) {

                Log.d("InputStream", e.getLocalizedMessage());

            }



            return result;

        }



        private  String convertInputStreamToString(InputStream inputStream) throws IOException {

            BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));

            String line = "";

            String result = "";

            while((line = bufferedReader.readLine()) != null)

                result += line;



            inputStream.close();

            return result;



        }

    }



}
 
結果: (此沒有設layout 不用想activity_main.xml要寫什麼)

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 程式小試身手 的頭像
    程式小試身手

    程式小試身手

    程式小試身手 發表在 痞客邦 留言(1) 人氣()