2012-01-23 55 views
0

我正在开发一个android应用,我要求显示用户当前位置和其他用途 在谷歌地图上当前位置附近。为了让用户在当前位置附近有单独的服务,我需要通过用户当前位置纬度/长只,android:在android地图上显示用户当前位置和当前位置附近的其他用户

我的问题是我如何获得用户当前的位置并显示在地图上(就像iphone与一些动画) 接近当前位置的所有其他用户(每个用户都有一个类型,根据不同的类型,我需要在地图上显示不同的标记/绘制)

任何想法或任何教程链接示例代码将帮助我

山姆

回答

4

首先你需要一个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); 
    }  
} 
+0

感谢代码,只有一个问题,我怎么能得到当前的位置,并显示我t与地图上的一些动画 – Sam

+0

我修改了我的答案,以包括这一点。 – fiestacasey

相关问题