我正在开发一个Android应用程序,我要求显示用户当前位置和其他用途 靠近谷歌地图上的当前位置。为了让用户在当前位置附近,我需要单独的服务来传递用户当前位置lat / long,
我的问题是我如何获取用户当前位置并在地图上显示(就像iphone带有一些动画) 所有其他用户靠近当前位置(每个用户都有一个类型,具体取决于我需要在地图上显示不同标记/可绘制的类型)
任何想法或任何教程链接示例代码都会帮助我
萨姆
答案 0 :(得分:6)
首先你需要一个mapview:
MapView mapView = (MapView) findViewById(R.id.mapView);
显示其他用户:
List<GeoPoint> otherUsers = .... from your service
for(GeoPoint user: otherUsers )
{
mapView .addOverlay(new MapOverlay(user);
}
将地图围绕用户居中:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location l = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//or if you want to use gps l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//you may want to use a different manner of getting the location since this may be out of date. see http://developer.android.com/reference/android/location/LocationListener.html for live data
point = new GeoPoint((int)(l.getLatitude()*1e6),(int)(l.getLongitude()*1e6));
mc.animateTo(point);
mapView.setBuiltInZoomControls(true);
mapView.invalidate();
需要显示其他用户的覆盖类:
public class MapOverlay extends Overlay {
private GeoPoint data;
public MapLineOverlay( GeoPoint item) {
data = item;
}
/* (non-Javadoc)
* @see com.google.android.maps.Overlay#draw(android.graphics.Canvas, com.google.android.maps.MapView, boolean, long)
*/
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when) {
Projection projection = mapView.getProjection();
if (shadow == false) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(data, point);
paint.setColor(color);
paint.setStrokeWidth(4);
canvas.drawPoint((float) point.x, (float) point.y, , paint);
}
return super.draw(canvas, mapView, shadow, when);
}
/* (non-Javadoc)
* @see com.google.android.maps.Overlay#draw(android.graphics.Canvas, com.google.android.maps.MapView, boolean)
*/
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
}
}