通常在这些代码挑战面试中,他们想看看您能在相对较短的时间内想出什么。
与@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);
}
}