Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package nl.timmevandermeer.carglass; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import com.google.android.glass.view.WindowUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; /** * Populates views in a {@code CardScrollView} with cards built from custom embedded layouts to * represent items in a simple table. */ public class LocationActivity extends Activity { private static final String TAG = LocationActivity.class.getSimpleName(); private static final String STATIC_MAP_URL_TEMPLATE = "https://maps.googleapis.com/maps/api/staticmap" + "?center=%.5f,%.5f" + "&zoom=%d" + "&sensor=true" + "&size=640x360" + "&scale=1" + "&style=element:geometry%%7Cinvert_lightness:true" + "&style=feature:landscape.natural.terrain%%7Celement:geometry%%7Cvisibility:on" + "&style=feature:landscape%%7Celement:geometry.fill%%7Ccolor:0x303030" + "&style=feature:poi%%7Celement:geometry.fill%%7Ccolor:0x404040" + "&style=feature:poi.park%%7Celement:geometry.fill%%7Ccolor:0x0a330a" + "&style=feature:water%%7Celement:geometry%%7Ccolor:0x00003a" + "&style=feature:transit%%7Celement:geometry%%7Cvisibility:on%%7Ccolor:0x101010" + "&style=feature:road%%7Celement:geometry.stroke%%7Cvisibility:on" + "&style=feature:road.local%%7Celement:geometry.fill%%7Ccolor:0x606060" + "&style=feature:road.arterial%%7Celement:geometry.fill%%7Ccolor:0x888888"; /** * Formats a Google static maps URL for the specified location and zoom level. */ private static String makeStaticMapsUrl(double latitude, double longitude, int zoom) { return String.format(STATIC_MAP_URL_TEMPLATE, 52.001491, 4.369600, 18); } private ImageView mMapView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMapView = new ImageView(this); setContentView(mMapView); loadMap(37.8019, -122.4189, 18); } /** * Load the map asynchronously and populate the ImageView when it's loaded. */ private void loadMap(double latitude, double longitude, int zoom) { String url = makeStaticMapsUrl(52.001783, 4.368871, 10); new AsyncTask<String, Void, Bitmap>() { @Override protected Bitmap doInBackground(String... urls) { try { HttpResponse response = new DefaultHttpClient().execute(new HttpGet(urls[0])); InputStream is = response.getEntity().getContent(); return BitmapFactory.decodeStream(is); } catch (Exception e) { Log.e(TAG, "Failed to load image", e); return null; } } @Override protected void onPostExecute(Bitmap bitmap) { if (bitmap != null) { mMapView.setImageBitmap(bitmap); } } }.execute(url); } }