TODO:對上一篇隨筆:Step by Step——Google Map View(Hello View) 進行進一步改進,使其能夠獲取設備當前位置,并顯示對應地圖
step1:定義LocationManager,獲取當前location,并封裝成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,獲取當前位置
locationManager.requestLocationUpdates(provider, 0, 0,
new MyLocationListener());
location = locationManager.getLastKnownLocation(provider);
}
GeoPoint point = null;
if (location == null) {
Double lat = 37.422006 * 1E6; // 默認值
Double lng = -122.084095 * 1E6;
point = new GeoPoint(lat.intValue(), lng.intValue());
} else // 當前反饋的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:在對應的Point上顯示標記
/**
* 顯示標記
*
* @param point
*/
private void displayMark(GeoPoint point) {
OverlayItem overlayitem = new OverlayItem(point, "我是個胖紙!", "我在這!");
itemizedoverlay.addOverlay(overlayitem);
}
注:為了不讓標記重復顯示,在ItemizedOverlay的子類里追加一個remove方法:
/**
* remove
*
* @param index
*/
public void removeOverlay(int index) {
mOverlays.remove(index);
populate();
}
修改
displayMark 方法:
/**
* 顯示標記
*
* @param point
*/
private void displayMark(GeoPoint point) {
OverlayItem overlayitem = new OverlayItem(point, "我是個胖紙!", "我在這!");
if (itemizedoverlay.size() != 0) {
itemizedoverlay.removeOverlay(0);
}
itemizedoverlay.addOverlay(overlayitem);
}
step3:獲得當前Location對用Point,將Point上加Mark,并將該圖層加到MapOverlays:
GeoPoint point = getCurrentGeoPoint();
displayMark(point);
mapOverlays.add(itemizedoverlay);
step4:調整Map狀態,顯示對應位置的地圖:
mapcontroller = mapView.getController();
mapcontroller.setCenter(point);
mapcontroller.setZoom(15);
mapcontroller.animateTo(point);
step5:定義LocationListener,監聽每次Location的變化,并實時的更新地圖上的顯示:
/**
* 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);
/**
* 是否顯示衛星圖
*/
mapView.setSatellite(false);
mapView.setBuiltInZoomControls(true);
locationManager = (LocationManager) GMapTest.this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new MyLocationListener());
/**
* 得到所有圖層對象
*/
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;
}
/**
* 顯示標記
*
* @param point
*/
private void displayMark(GeoPoint point) {
OverlayItem overlayitem = new OverlayItem(point, "我是個胖紙!", "我在這!");
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,獲取當前位置
locationManager.requestLocationUpdates(provider, 0, 0,
new MyLocationListener());
location = locationManager.getLastKnownLocation(provider);
}
GeoPoint point = null;
if (location == null) {
Double lat = 37.422006 * 1E6; // 默認值
Double lng = -122.084095 * 1E6;
point = new GeoPoint(lat.intValue(), lng.intValue());
} else // 當前反饋的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之上,創建一個圖層(OverlayItem) 生成該類對象,并將該對象添加到MapView.getOverlays()里
* 一個OverlayItem對象就代表了一個在地圖上顯示的標記
*
* @author Ying_er
* @Email melody.crazycoding@gmail.com
* @time 2011/10/12 14:53:17
* @version 1.00
*/
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
/**
* 創建一個List對象,存儲該圖層中所有的標記對象
*/
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context;
/**
* 參數用于指定顯示標記的默認圖片
*
* @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;
}
/**
* 創建一個OverlayItem對象
*/
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
/**
* 返回當前Overlay中的OverlayItem對象個數
*/
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
/**
* 將生成好的OverlayItem對象添加到List當中
*
* @param overlay
*/
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
/**
* remove
*
* @param index
*/
public void removeOverlay(int index) {
mOverlays.remove(index);
populate();
}
/**
* 當用戶點擊標記時所執行的操作
*/
@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)
評論(0) 編輯 收藏 所屬分類:
Google Map 、
Android