2015-03-13 100 views
0

在面試前我已經給出了這個練習來創建一個小應用程序。 在應用程序的某個位置,我需要使用緯度和經度座標在地圖上顯示一個位置。在不使用谷歌地圖的情況下顯示地圖上的位置

但我認爲我不應該使用Google Maps API,而是使用設備上的「內置」地圖。

有這樣的事嗎? 是否有一個簡單的MapView視圖,我可以在我的佈局中接受緯度和經度座標,並在正確的位置放置一個標記?

我一直在尋找一個教程,解釋如何在Android上使用內置的地圖API,但找不到一個。

任何幫助,將不勝感激。

回答

1

通常在這些代碼挑戰面試時,他們想看看你能在相對較短的時間內想出什麼。

與@bwegs提到的類似,你應該向面試官詢問是否有任何限制。我已經做了很多面試,其中的代碼挑戰是在24小時內創建一些東西,但應用程序的大小隻是大到完成。在這種情況下,我會利用第三方庫。

我不知道檢索地圖,所以如果你可以使用谷歌地圖API,比我想先在這裏閱讀文檔https://developers.google.com/maps/documentation/android/

而且你可以利用谷歌靜態地圖API https://developers.google.com/maps/documentation/staticmaps/的啓動任何其他方法這將返回特定位置的靜態圖像。

這裏是我創建得到一個谷歌靜態地圖

class CreateStaticMapAsyncTask extends AsyncTask<String, Void, Bitmap> { 

    private static final String STATIC_MAPS_API_BASE = "https://maps.googleapis.com/maps/api/staticmap"; 
    private static final String STATIC_MAPS_API_SIZE = "500x500"; 

    @Override 
    protected void onPreExecute() { 
     addTask(); // adds one to task count. 
     super.onPreExecute(); 

    } 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     locationString = params[0]; 
     Bitmap bmp = null; 
     StringBuilder sb = new StringBuilder(STATIC_MAPS_API_BASE); 
     try { 
      sb.append("?center=").append(
        URLEncoder.encode(locationString, "UTF-8")); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     sb.append("&size=" + STATIC_MAPS_API_SIZE); 
     sb.append("&key=" + API_KEY); 
     String url = new String(sb.toString()); 
     Log.e("URL", sb.toString()); 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(url); 

     InputStream in = null; 
     try { 
      in = httpclient.execute(request).getEntity().getContent(); 
      bmp = BitmapFactory.decodeStream(in); 
      in.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return bmp; 

    } 

    protected void onPostExecute(Bitmap bmp) { 
     super.onPostExecute(bmp); 
     if (bmp != null) { 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
      data = stream.toByteArray(); 
      removeTask(); 
      allTasksComplete(); 

     } 
    } 
} 

可與該電話new CreateStaticMapAsyncTask().execute(loc);

,你可以檢索例如當前位置,像這樣(被訪問的一般的AsyncTask不是唯一的方式)

LocationManager locManager = (LocationManager) getActivity() 
        .getSystemService(Context.LOCATION_SERVICE); 
      boolean network_enabled = locManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
      Location location; 

      if (network_enabled) { 
       location = locManager 
         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       if (location != null) { 

        _longitude = location.getLongitude(); 
        _latitude = location.getLatitude(); 
        etLocation.setText(_latitude + "," + _longitude); 
       } 
      } 
+0

感謝ItzHoudini。我想我會使用Google Maps API。這是一個初學者的位置,所以我想他們希望我使用標準方法。我只是覺得還有另一種流行的做法。 – 2015-03-13 20:45:42

+0

我從您提供的鏈接中的指南瞭解到,我必須註冊我的應用以獲取使用Google Maps API的API密鑰。但我甚至不發佈應用程序。這只是一個練習。這真的是唯一的選擇嗎? – 2015-03-13 21:25:08

+1

我相信你不得不使用API​​密鑰。 Google Maps API V3不需要密鑰,但僅適用於Javascript。您應該瞭解如何檢索SHA1指紋,並設置Google Developer Console帳戶。理解您所看到的內容需要一點時間,但是值得,因爲在您的職業生涯中您會遇到多個Google API。 – 2015-03-14 01:45:19

2

當今Android設備上的「內置」地圖谷歌地圖。畢竟,Android = Google。我無法想象爲什麼有人會試圖讓你做這個而不是某種API,但這是另一回事。

不要害怕與面試官(或者給你這個練習的人)覈實你是否可以使用谷歌地圖API - 這是一個非常重要的細節,可以讓你的生活變得非常重要更輕鬆。

+1

好的,謝謝你的提示! – 2015-03-13 20:40:02