- package com.google.mygooglemap;
- import java.util.List;
- import android.content.Intent;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.RectF;
- import android.location.Address;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.Toast;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.MapActivity;
- import com.google.android.maps.MapController;
- import com.google.android.maps.MapView;
- import com.google.android.maps.Overlay;
- import com.google.android.maps.Projection;
- public class MyGoogleMapActivity extends MapActivity implements LocationListener
- {
- static final int INITIAL_ZOOM_LEVEL = 20;
- static final int INITIAL_LATITUDE = 25040255; //起始緯度
- static final int INITIAL_LONGITUDE = 121512377; //起始經度
- static final int QRCODE_REQUEST = 1; //QRcode功能的request識別碼
-
- private MapView mapView;
- private MapController mapControl;
- private LocationManager mLocationManager;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mapView = (MapView)findViewById(R.id.mapview);
- mapView.setBuiltInZoomControls(true); //設定開啟內建的放大縮小控制
-
- //地圖上可以使用overlay來貼圖或寫字,在此我們將自己建立的Overlay類別貼上
- GeoPoint center = new GeoPoint(INITIAL_LATITUDE,INITIAL_LONGITUDE); //建立地圖中心點的座標
- MyPointOverlay myPointOverlay = new MyPointOverlay(center); //以地圖中心點的座標來建議我們的Overlay物件
- mapView.getOverlays().add(myPointOverlay); //將我們的Overlay物件加入mapView
-
- mapControl = mapView.getController(); //取得mapView的控制器G
- mapControl.setZoom(INITIAL_ZOOM_LEVEL); //設定地圖大小
- mapControl.setCenter(center); //設定地圖中心點
-
- mLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
- //取得系統服務中的位置服務管理
- mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
- //設定由GPS來提供位置更新
- mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
- //設定由network來提供位置更新
- }
-
- public void onLocationChanged(Location location)
- {
- GeoPoint center = new GeoPoint((int)(location.getLatitude()*1E6),
- (int)(location.getLongitude()*1E6));
- MyPointOverlay myPointOverlay = new MyPointOverlay(center);
- mapView.getOverlays().clear();
- mapView.getOverlays().add(myPointOverlay);
- //將地圖的中心點以動畫的方式移動到center的位置
- mapControl.animateTo(center);
- }
- public void onProviderDisabled(String provider)
- {}
- public void onProviderEnabled(String provider)
- {}
- public void onStatusChanged(String provider, int status,Bundle extras)
- {}
- @Override
- protected boolean isRouteDisplayed()
- {
- return false;
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) //按下選單鈕的回呼函式
- {
- super.onCreateOptionsMenu(menu);
- MenuInflater inflater = getMenuInflater(); //建立一個選單氣泡
- inflater.inflate(R.menu.menu, menu); //打開選單氣泡
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) //按下選單選項的回呼函式
- {
- switch(item.getItemId())
- {
- case R.id.capture:
- try
- {
- Intent intent = new Intent("com.google.zxing.client.android.SCAN"); //開啟條碼掃描器
- intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); //設定QR Code參數
- startActivityForResult(intent, QRCODE_REQUEST);
- return true;
- }
- catch(Exception e)
- {
- Toast.makeText(this, "呼叫條碼掃描器失敗,請先確定是否正確安裝!", Toast.LENGTH_LONG).show();
- }
- break;
- }
- return false;
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) //呼叫Activity後的回呼函式
- {
- super.onActivityResult(requestCode, resultCode, data);
-
- if (requestCode == QRCODE_REQUEST) //判斷是哪一個Activity回呼的
- { //startActivityForResult回傳值
- if (resultCode == RESULT_OK)
- {
- String contents = data.getStringExtra("SCAN_RESULT"); //取得QR Code內容
- //Toast.makeText(this, contents, Toast.LENGTH_SHORT).show();
- Geocoder geocoder = new Geocoder(this);
- try
- {
- List<Address> addr = geocoder.getFromLocationName(contents,1);
- Address address = addr.get(0);
- GeoPoint center = new GeoPoint((int)(address.getLatitude()*1E6),
- (int)(address.getLongitude()*1E6));
- MyPointOverlay myPointOverlay = new MyPointOverlay(center);
- mapView.getOverlays().clear();
- mapView.getOverlays().add(myPointOverlay);
- //將地圖的中心點以動畫的方式移動到center的位置
- mapControl.animateTo(center);
- }
- catch(Exception e)
- {
- Toast.makeText(this, "地址:『"+contents+"』查詢失敗", Toast.LENGTH_LONG).show();
- }
- }
- }
- }
- }
- class MyPointOverlay extends Overlay
- {
- private GeoPoint mGeoPoint;
- private int mRadius = 7; //設定大圓的半徑
- private int sRadius = 5; //設定小圓的半徑
-
- public MyPointOverlay(GeoPoint gp)
- {
- mGeoPoint = gp;
- }
-
- @Override
- public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when)
- {
- //if (shadow == false)
- //{
- Paint paint = new Paint(); //新增畫筆
- paint.setAntiAlias(true); //設定畫筆鋸齒消除
- paint.setColor(Color.RED); //將畫筆的顏色設為紅色
-
- Point pointDraw = new Point(); //建立一個畫布座標點
- Projection projection = mapView.getProjection(); //取得地圖的元件的畫布投影器
- projection.toPixels(mGeoPoint, pointDraw); //將地圖上的經緯度座標轉換到畫布上的座標
-
- RectF ovalPoint = new RectF(pointDraw.x - mRadius, //建立畫圓的矩形範圍(Float)
- pointDraw.y - mRadius,
- pointDraw.x + mRadius,
- pointDraw.y + mRadius);
- canvas.drawOval(ovalPoint, paint); //在畫布上畫圓
-
- paint.setColor(Color.BLUE);
- ovalPoint = new RectF(pointDraw.x - sRadius,
- pointDraw.y - sRadius,
- pointDraw.x + sRadius,
- pointDraw.y + sRadius);
- canvas.drawOval(ovalPoint, paint);
- //}
- return super.draw(canvas, mapView, shadow, when);
- }
- }
複製代碼 |