【问题标题】:Displaying a location on a map without using Google Maps不使用 Google 地图在地图上显示位置
【发布时间】:2015-05-16 10:47:02
【问题描述】:

我在面试之前接受了这个练习,以创建一个小应用程序。 在应用程序的某个位置,我需要使用经纬度坐标在地图上显示一个位置。

但我认为我不应该使用 Google Maps API,而应该使用设备上的“内置”地图。

有这种事吗? 是否有一个简单的 MapView 视图可以放入我的布局中,它接受纬度和经度坐标并在正确的位置放置一个标记?

我一直在寻找解释如何在 Android 上使用内置地图 API 的教程,但找不到。

任何帮助将不胜感激。

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    通常在这些代码挑战面试中,他们想看看您能在相对较短的时间内想出什么。

    与@bwegs 提到的类似,您应该与面试官确认是否有任何限制。我做过很多采访,其中的代码挑战是在 24 小时内创建一些东西,但应用程序的规模太大而无法完成。在这种情况下,我会使用第三方库。

    我不知道任何其他检索地图的方法,所以如果您可以使用 Google Maps API,我会先阅读此处的文档https://developers.google.com/maps/documentation/android/

    您还可以利用 Google Static Maps API https://developers.google.com/maps/documentation/staticmaps/,它会返回特定位置的静态图像。

    这是我为获取 Google 静态地图而创建的通用 AsyncTask

    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);访问它

    例如,您可以像这样检索您当前的位置(不是唯一的方法)

    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);
                    }
                }
    

    【讨论】:

    • 感谢 ItzHoudini。我想我会使用 Google Maps API。这是初学者的职位,所以我想他们希望我使用标准方法。我只是认为还有另一种流行的方法。
    • 我从您提供的链接中的指南了解到,我必须注册我的应用以获得 API 密钥才能使用 Google Maps API。但我什至没有发布应用程序。这只是一个练习。这真的是唯一的选择吗?
    • 不幸的是,我相信您必须使用 API 密钥。 Google Maps API V3 不需要密钥,但它仅适用于 Javascript。您应该研究如何检索您的 SHA1 指纹,并设置一个 Google Developer Console 帐户。了解您正在查看的内容需要一点时间,但这是值得的,因为您会在职业生涯中遇到多个 Google API。
    【解决方案2】:

    如今,Android 设备上的“内置”地图 Google 地图。毕竟,Android = Google。我无法想象为什么有人会试图让你在没有某种 API 的情况下做到这一点,但那是另一回事了。

    不要害怕与面试官(或为您提供此练习的人)核实您是否可以使用 Google Maps API - 这是一个重要的细节,可以让您的生活变得更轻松。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2018-11-12
      • 1970-01-01
      相关资源
      最近更新 更多