標題:
Google地圖程式正確版本及注意事項
[打印本頁]
作者:
ray
時間:
2012-11-10 18:15
標題:
Google地圖程式正確版本及注意事項
本帖最後由 ray 於 2012-11-10 18:33 編輯
[attach]624[/attach]
關於我們上課遇到的問題說明整理如下:
1.執行看得見地圖,但是無法縮放移動,是因為layout的xml裡面mapview的設定我忘了加入:android:clickable="true"這個屬性
2.執行看的見定位後的地圖,但是把onLocationChanged中的程式註解後看不見初始的地圖,是因為我們第一次執行時他成功並將地圖資料下載在快取中
後來因為layout的xml裡面mapview的apiKey被誤判為錯誤的,所以不再能取得地圖資料,解法:將xml刪掉重建然後clean讓R.java重新產生即可!
3.模擬器無法執行的問題看起來是android 4.1(API 16)的模擬器本身的問題,這一點我無法從網路或朋友處得到確定的資料,但是看來目前最穩定的版本
還是android 2.2(API 8)
我後來下載了android 2.2(API 8)的SDK並重寫了我們上課教過的程式,目前不管是在模擬器或手機皆能正確執行了,程式跟我們上課講的除了變數的定義有
很小的差異外,其餘應該是一模一樣了,不過本來提出成updateMap方法的部分我後來覺得還是有些差異,所以就用回原來不提出的方法了!
除此之外我也把上課時對程式逐行的解說寫了大略的註解,各位可以下載回去複習!!
有空的同學(特別是自己帶NB的),可以先利用SDK Manager將android 2.2(API 8)全部下載下來再import我上傳的專案來測試,應該沒問題了!
其餘的同學也不用擔心,下週上課我還是會詳細說明這個程式的~不過如果上課時就覺得很吃力的同學還是強烈建議即便無法執行也先下載回去複習一下~
作者:
ray
時間:
2012-11-12 19:32
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);
}
}
複製代碼
作者:
ray
時間:
2012-11-12 19:32
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);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2