首页 文章

在不使用Google Map 的情况下在 Map 上显示位置

提问于
浏览
0

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

但我认为我不应该使用Google Maps API,而是使用设备上的“内置” Map .

有这样的事吗?是否有一个简单的MapView视图,我可以放在我的布局中,接受lat和lon coords并在正确的位置放置一个标记?

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

任何帮助,将不胜感激 .

2 回答

  • 1

    通常,在这些代码挑战面试的情况下,他们希望看到您能够在相对较短的时间内提出的内容 .

    类似于@bwegs所提到的,你应该与面试官核实是否有任何限制 . 我做了很多采访,其中代码挑战是在24小时内创建一些东西,但是应用程序的大小只是大到完成 . 在这种情况下,我会使用第三方库 .

    我不知道任何其他检索 Map 的方法,所以如果你可以使用Google Maps API,那么我首先阅读这里的文档https://developers.google.com/maps/documentation/android/

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

    这是我为获取Google静态 Map 而创建的一般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);
                    }
                }
    
  • 2

    现在,Android设备上的"built-in" Map 是Google Map . 毕竟,Android = Google . 我无法想象为什么有人会试图让你这样做 without 某种API,但这是另一个故事 .

    不要害怕与面试官(或任何给你这个练习的人)核实你是否可以使用谷歌 Map API - 这是一个重要的细节,可以使你的生活更轻松 .

相关问题