这是一个功能综合的案例
主要的功能就是:获取手机的定位信息,然后以图标的形式显示在地图上,点击地图,就会弹出相应的信息。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/map_view1"
android:clickable="true"
android:apiKey="0zoMLU7jtw20HSV-bpo6p81D6rFEvGwMz2BJqiw"
/>
</LinearLayout>
----------------------------------------------------------------------------------------------------------------------------------------------
package com.example.googlemap;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
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;
/*
* 这个Activity的主要的功能是:
* 获取用户的具体的经纬度,然后在地图上显示
*
* */
public class ShowPositionInGoogleMap extends MapActivity {
//主要管理用户的位置信息
private LocationManager locationManager;
private MapView mapView ;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.showpositioningooglemap);
//获取地图显示的控件
mapView=(MapView)findViewById(R.id.map_view1);
mapView.setStreetView(true);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
MapController mapcontroller=mapView.getController();
GeoPoint point=getCurrentPoint();// 设置地图的当前位置信息
mapcontroller.setCenter(point);
mapcontroller.setZoom(20);
mapcontroller.animateTo(point);
//接下来我们需要在地图上显示我们用户所在的位置 使用图层的概念
/*
* OverLay是一个抽象类
* 子类ItemizedOverlay
* 使用图层的几个基本的步骤
* (1)创建一个类 extends ItemizedOverLay
* (2)创建OverlayItem对象,将OverLayItem对象添加到自己创建的Overlay中
* (3)将该类添加到MapView。getOverlays()的集合中
* */
//获取资源文件
Drawable drawable=getResources().getDrawable(R.drawable.ic_launcher);
//创建自己的图层
FirstOverlay overlay=new FirstOverlay(drawable,this);
//创建自己的图标
OverlayItem overlayitem=new OverlayItem(point, "title", "message");
//将图标加到图层上去
overlayitem.setMarker(drawable);
overlay.addOverlayItem(overlayitem);
List<Overlay> list=mapView.getOverlays();
//将自己的图层加到地图的上面
list.add(overlay);
}
/*
* 获取当前用户的具体位置
* */
private GeoPoint getCurrentPoint()
{
//获取LocalManager对象
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//获取本地手机最好的提供定位的方法
String provider=this.getBestProvider();
//获取用户最后一次的经纬度的信息
Location location=locationManager.getLastKnownLocation(provider);
//如果没有用户的位置信息
if(location==null){
//我们需要更新用户的位置信息,使用监听函数进行处理
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
{
Double lat=location.getLatitude()*1E6;
Double lng=location.getLongitude()*1E6;
point=new GeoPoint(lat.intValue(),lng.intValue());
}
return point;
}
/*
* 创建一个用户位置移动的监听器
*
* */
class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
System.out.println(location.getLatitude());
System.out.println(location.getLongitude());
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
//获取满足用户要求的条件,返回手机内部提供的定位服务
private String getBestProvider(){
Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.ACCURACY_HIGH);
return locationManager.getBestProvider(criteria, true);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}