TODO:對(duì)上一篇隨筆:Step by Step——Google Map View(Hello View) 進(jìn)行進(jìn)一步改進(jìn),使其能夠獲取設(shè)備當(dāng)前位置,并顯示對(duì)應(yīng)地圖
step1:定義LocationManager,獲取當(dāng)前l(fā)ocation,并封裝成GeoPoint
locationManager = (LocationManager) GMapTest.this
.getSystemService(Context.LOCATION_SERVICE);
/**
* get Point
*
* @return
*/
private GeoPoint getCurrentGeoPoint() {
String context = Context.LOCATION_SERVICE;
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
if (location == null) { // 沒有最后位置,更新gps,獲取當(dāng)前位置
locationManager.requestLocationUpdates(provider, 0, 0,
new MyLocationListener());
location = locationManager.getLastKnownLocation(provider);
}
GeoPoint point = null;
if (location == null) {
Double lat = 37.422006 * 1E6; // 默認(rèn)值
Double lng = -122.084095 * 1E6;
point = new GeoPoint(lat.intValue(), lng.intValue());
} else // 當(dāng)前反饋的GPS位置
{
Double lat = location.getLatitude() * 1E6;
Double lng = location.getLongitude() * 1E6;
point = new GeoPoint(lat.intValue(), lng.intValue());
System.out.println(lat);
System.out.println(lng);
}
return point;
}
step2:在對(duì)應(yīng)的Point上顯示標(biāo)記
/**
* 顯示標(biāo)記
*
* @param point
*/
private void displayMark(GeoPoint point) {
OverlayItem overlayitem = new OverlayItem(point, "我是個(gè)胖紙!", "我在這!");
itemizedoverlay.addOverlay(overlayitem);
}
注:為了不讓標(biāo)記重復(fù)顯示,在ItemizedOverlay的子類里追加一個(gè)remove方法:
/**
* remove
*
* @param index
*/
public void removeOverlay(int index) {
mOverlays.remove(index);
populate();
}
修改
displayMark 方法:
/**
* 顯示標(biāo)記
*
* @param point
*/
private void displayMark(GeoPoint point) {
OverlayItem overlayitem = new OverlayItem(point, "我是個(gè)胖紙!", "我在這!");
if (itemizedoverlay.size() != 0) {
itemizedoverlay.removeOverlay(0);
}
itemizedoverlay.addOverlay(overlayitem);
}
step3:獲得當(dāng)前Location對(duì)用Point,將Point上加Mark,并將該圖層加到MapOverlays:
GeoPoint point = getCurrentGeoPoint();
displayMark(point);
mapOverlays.add(itemizedoverlay);
step4:調(diào)整Map狀態(tài),顯示對(duì)應(yīng)位置的地圖:
mapcontroller = mapView.getController();
mapcontroller.setCenter(point);
mapcontroller.setZoom(15);
mapcontroller.animateTo(point);
step5:定義LocationListener,監(jiān)聽每次Location的變化,并實(shí)時(shí)的更新地圖上的顯示:
/**
* LocationListener
*
* @author Ying_er
* @Email melody.crazycoding@gmail.com
* @time 2011/10/14 9:30:51
* @version 1.00
*/
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Double lat = location.getLatitude() * 1E6;
Double lng = location.getLongitude() * 1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
mapcontroller.setCenter(point);
mapcontroller.animateTo(point);
displayMark(point);
System.out.println("locationChanged");
System.out.println(location.getLatitude());
System.out.println(location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new MyLocationListener());
附:MapActivity(GMapTest)完整代碼:
package com.yinger;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
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.OverlayItem;
/**
*
* @author Ying_er
* @Email melody.crazycoding@gmail.com
* @time 2011/10/13 10:37:10
* @version 1.00
*/
public class GMapTest extends MapActivity {
MapView mapView = null;
LocationManager locationManager = null;
MapController mapcontroller = null;
HelloItemizedOverlay itemizedoverlay = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
/**
* 是否顯示街道信息
*/
mapView.setStreetView(true);
/**
* 是否顯示交通信息
*/
mapView.setTraffic(true);
/**
* 是否顯示衛(wèi)星圖
*/
mapView.setSatellite(false);
mapView.setBuiltInZoomControls(true);
locationManager = (LocationManager) GMapTest.this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new MyLocationListener());
/**
* 得到所有圖層對(duì)象
*/
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.androidmarker);
itemizedoverlay = new HelloItemizedOverlay(drawable, this);
GeoPoint point = getCurrentGeoPoint();
displayMark(point);
mapOverlays.add(itemizedoverlay);
mapcontroller = mapView.getController();
mapcontroller.setCenter(point);
mapcontroller.setZoom(15);
mapcontroller.animateTo(point);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/**
* 顯示標(biāo)記
*
* @param point
*/
private void displayMark(GeoPoint point) {
OverlayItem overlayitem = new OverlayItem(point, "我是個(gè)胖紙!", "我在這!");
if (itemizedoverlay.size() != 0) {
itemizedoverlay.removeOverlay(0);
}
itemizedoverlay.addOverlay(overlayitem);
}
/**
* get Point
*
* @return
*/
private GeoPoint getCurrentGeoPoint() {
String context = Context.LOCATION_SERVICE;
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
if (location == null) { // 沒有最后位置,更新gps,獲取當(dāng)前位置
locationManager.requestLocationUpdates(provider, 0, 0,
new MyLocationListener());
location = locationManager.getLastKnownLocation(provider);
}
GeoPoint point = null;
if (location == null) {
Double lat = 37.422006 * 1E6; // 默認(rèn)值
Double lng = -122.084095 * 1E6;
point = new GeoPoint(lat.intValue(), lng.intValue());
} else // 當(dāng)前反饋的GPS位置
{
Double lat = location.getLatitude() * 1E6;
Double lng = location.getLongitude() * 1E6;
point = new GeoPoint(lat.intValue(), lng.intValue());
System.out.println(lat);
System.out.println(lng);
}
return point;
}
/**
* LocationListener
*
* @author Ying_er
* @Email melody.crazycoding@gmail.com
* @time 2011/10/14 9:30:51
* @version 1.00
*/
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Double lat = location.getLatitude() * 1E6;
Double lng = location.getLongitude() * 1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
mapcontroller.setCenter(point);
mapcontroller.animateTo(point);
displayMark(point);
System.out.println("locationChanged");
System.out.println(location.getLatitude());
System.out.println(location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
}
ItemizedOverlay(HelloItemizedOverlay)完整代碼:
package com.yinger;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
/**
* 在MapView之上,創(chuàng)建一個(gè)圖層(OverlayItem) 生成該類對(duì)象,并將該對(duì)象添加到MapView.getOverlays()里
* 一個(gè)OverlayItem對(duì)象就代表了一個(gè)在地圖上顯示的標(biāo)記
*
* @author Ying_er
* @Email melody.crazycoding@gmail.com
* @time 2011/10/12 14:53:17
* @version 1.00
*/
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
/**
* 創(chuàng)建一個(gè)List對(duì)象,存儲(chǔ)該圖層中所有的標(biāo)記對(duì)象
*/
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context;
/**
* 參數(shù)用于指定顯示標(biāo)記的默認(rèn)圖片
*
* @param arg0
*/
public HelloItemizedOverlay(Drawable arg0) {
super(boundCenterBottom(arg0));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable arg0, Context context) {
super(boundCenterBottom(arg0));
// TODO Auto-generated constructor stub
this.context = context;
}
/**
* 創(chuàng)建一個(gè)OverlayItem對(duì)象
*/
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
/**
* 返回當(dāng)前Overlay中的OverlayItem對(duì)象個(gè)數(shù)
*/
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
/**
* 將生成好的OverlayItem對(duì)象添加到List當(dāng)中
*
* @param overlay
*/
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
/**
* remove
*
* @param index
*/
public void removeOverlay(int index) {
mOverlays.remove(index);
populate();
}
/**
* 當(dāng)用戶點(diǎn)擊標(biāo)記時(shí)所執(zhí)行的操作
*/
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
posted on 2011-10-14 10:01
Ying-er 閱讀(1582)
評(píng)論(0) 編輯 收藏 所屬分類:
Google Map 、
Android