No Result
View All Result
No Result
View All Result
No Result
View All Result

Use Google’s Static Maps API to illustrate location in your Android apps

king by king
October 31, 2022
in Android, Tutorial
0
Use Google’s Static Maps API to illustrate location in your Android apps

This tutorial shows how simple it is to use Google’s free static map SAAS in your Android apps if the Google Maps API requires too much overhead for the desired functionality.

Most developers are familiar with the Google Maps API. In my opinion, its popularity is rooted in the data it serves up. Google Maps tends to be accurate and pretty quick when responding to millions of requests from all around the world.

However, it’s not that easy to work with the Google Maps API. At times I dread going through the associated overhead when the only functionality I need is a static map image of a particular location. Recently I stumbled across another Google Maps related API that is specifically for serving up static images based on a latitude / longitude pair. The best part is it’s dead simple to use.

Follow along with the tutorial below, or download and import the entire project directly into Eclipse. Ready?

1. Create a new Android project in Eclipse. Target SDK 14 (ICS) or better.

2. In your manifest file, you’ll need to add the internet permission.

AndroidManifest.xml

3. Our layout consists of a text view and an image view wrapped by a linear layout. Make the needed modifications in your /res/layouts folder.

activity_main.xml

 

4. We need to set our image view source to the remote URL that is the Google Maps endpoint. We do this in the /src/MainActivity.java file. For the purpose of this demonstration, I’m doing all the work in the on create override. Note that, because the image needs to be downloaded, we have to throw that processing off onto a background thread to keep our UI from stuttering on startup.

MainActivity.java
package com.example.googlestaticmapssaas;

import java.io.InputStream;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {

protected static final String STATIC_MAP_API_ENDPOINT = “http://maps.google.com/maps/api/staticmap?center=37.4223662,-122.0839445&zoom=15&size=200×200&sensor=false”;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncTask setImageFromUrl = new AsyncTask(){
@Override
protected Bitmap doInBackground(Void… params) {
Bitmap bmp = null;
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(STATIC_MAP_API_ENDPOINT);

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) {
if (bmp!=null) {
final ImageView iv = (ImageView) findViewById(R.id.img);
iv.setImageBitmap(bmp);
}
}
};
setImageFromUrl.execute();
}
},>,>

That’s all there is to it. Though before you copy and paste the code directly into your next app, be sure to read Google’s usage and API key requirements. It’s pretty generous and in most cases should be just fine, but it’s always a good idea to know what you’re agreeing to before your app tops the charts.

Share this:

  • Facebook
  • Tweet
  • WhatsApp

Related posts:

Netflix Games on Android

Enhanced Security Algorithms on the Pixel 6

Google Brings Android Games to Windows 10 and Windows 11

Tags: addadsAndroidAndroidManifest.xmlAPIappappsbasecallclientcodeCreatedatadevdevelopdeveloperEclipsefamiliarfilefinalfindFollowfreefungoodGoogleGooglesillustrateinternetkeylayoutlocationlosemanifestMapsmillionmillionspermissionpinpointpopularpopularityportProprotectpublicraceraterespondreturnridrootedsensorshowshowssimpleStaticsynctextThetimetimestipUIversionViewwayswidgetwork
Previous Post

Utter your commands, and Android will respond

Next Post

Pro tip: Create home screen folders with ASTRO File Manager

Next Post
Pro tip: Create home screen folders with ASTRO File Manager

Pro tip: Create home screen folders with ASTRO File Manager

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Posts

  • Netflix Games on Android
  • Enhanced Security Algorithms on the Pixel 6
  • Google Brings Android Games to Windows 10 and Windows 11
  • Oppo’s Find N Foldable: Next Generation of Smartphones
  • Huawei’s P50 Pocket: The Ultimate Portable Device

Categories

  • Android
  • Review
  • Tutorial
MicroG

  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms and Condition

Follow Us

No Result
View All Result
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms and Condition