Google靜態地圖下載

這個範例是用來抓取我們所要求的經緯度座標的地圖,在經度與緯度的文字方塊內輸入座標,按下載入的時候,會向GoogleMap伺服器索取地圖圖片,如果索取失敗會顯示錯誤訊息。
LoadStaticMap.JPG
圖1-6
以下為Google靜態地圖下載的程式碼:
專案下載:LoadStaticMap.rar
程式: GetGoogleMap.java

package LoadStaticMap.a;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.net.URL;
import java.net.URLConnection;
 
public class GetGoogleMap
{    
    // 取得GoogleMap
    public static Bitmap getMap(double Longitude,double Latitude,int width,int height)
    {
        try
        {
            String key = "ABQIAAAAz2rjkrilGZTguphQT5RQqxQnA14KtgqpIfJzkpdB1-oq1QYeLRTl0Szkt5Yli5Jv9oBOrf85JNpgIw";// "ABQIAAAAz2rjkrilGZTguphQT5RQqxQQIe5j7nozdTIq37ll2BjQmE8XsxTUxidRqBWWrDXy2cwQU686YRJCTQ";
            int zoom = 19;
            String size = String.format("%1sx%2s", width, height); 
 
            // 網址
            String urlString = String.format("http://maps.google.com/staticmap?center=%1$s,%2$s&zoom=%3$s&size=%4$s&maptype=hybrid&sensor=false&key=%5$s", Latitude, Longitude, zoom, size,key);
            //String urlString = "http://maps.google.com/staticmap?center="+Latitude+","+Longitude+"&zoom="+zoom+"&size=320x480&maptype=hybrid&sensor=false&key=" + key;
            //URL url = new URL(urlString);
            // use proxy server
            URL url = new URL("http","proxy1.internal",3128, urlString);
 
            // 連線
            URLConnection conn = url.openConnection();
            conn.connect();
 
            // 取得圖片串流
            Bitmap tempBitmap = BitmapFactory.decodeStream(conn.getInputStream());
 
            if (tempBitmap != null)
                return tempBitmap;
            else
                return null;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

程式: LoadStaticMap.java
package LoadStaticMap.a;
 
import LoadStaticMap.a.GetGoogleMap;
import LoadStaticMap.a.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
public class LoadStaticMap extends Activity {
    /** Called when the activity is first created. */
 
    // 文字方塊初始化
    private EditText lngText;
    private EditText latText;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        // 初始化按鈕
        final Button button = (Button) findViewById(R.id.widget32);
        button.setText("載入");
 
        // 初始化顯示文字
        final TextView lngView = (TextView) findViewById(R.id.widget33);
        final TextView latView = (TextView) findViewById(R.id.widget34);
 
        lngView.setText("經度:");
        latView.setText("緯度:");
 
        // 初始化文字方塊
        lngText = (EditText) findViewById(R.id.widget28);
        latText = (EditText) findViewById(R.id.widget29);
 
        // 設定經緯度  
        lngText.setText("118.32198666666666");
        latText.setText("24.449396666666669");
 
        // 初始化圖片方塊
        final ImageView imageView = (ImageView) findViewById(R.id.widget35);       
 
        // 設定按鈕事件
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                double lng;
                double lat;
 
                try{
                    lng = Double.parseDouble(lngText.getText().toString());
                    lat = Double.parseDouble(latText.getText().toString());
                }
                catch(Exception ex)
                {
                    Toast.makeText(LoadStaticMap.this, "輸入錯誤!",Toast.LENGTH_LONG).show();
 
                    // 發生錯誤則跳出
                    return ;
                }
 
                // 顯示訊息
                Toast.makeText(LoadStaticMap.this, "取得" + lng + "," + lat,Toast.LENGTH_LONG ).show();
 
                // 取得GoogleMap
                Bitmap tempBitmap = GetGoogleMap.getMap(lng,lat,imageView.getWidth(),imageView.getHeight());
 
                // 顯示地圖
                if (tempBitmap!=null)
                    imageView.setImageBitmap(tempBitmap);
 
                else
                    Toast.makeText(LoadStaticMap.this, "取得失敗",Toast.LENGTH_LONG ).show();                    
            }
        });
    }
}

Activity介面程式碼
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/widget28"
android:layout_width="200px"
android:layout_height="wrap_content"
android:text="EditText"
android:textSize="18sp"
android:layout_x="45px"
android:layout_y="9px"
>
</EditText>
<EditText
android:id="@+id/widget29"
android:layout_width="200px"
android:layout_height="44px"
android:text="EditText"
android:textSize="18sp"
android:layout_x="45px"
android:layout_y="53px"
>
</EditText>
<Button
android:id="@+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="251px"
android:layout_y="10px"
>
</Button>
<TextView
android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="1px"
android:layout_y="20px"
>
</TextView>
<TextView
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="1px"
android:layout_y="64px"
>
</TextView>
<ImageView
android:id="@+id/widget35"
android:layout_width="320px"
android:layout_height="320px"
android:layout_x="9px"
android:layout_y="115px"
>
</ImageView>
</AbsoluteLayout>

程式影片解說