我无法标记当前位置...应用程序总是得到消息“无法确定位置”。我在三星 Galaxy mini 上运行应用程序,我打开了 GPS :(
类 FixedMyLocationOverlay
公共类 FixedMyLocationOverlay 扩展 MyLocationOverlay { private boolean bugged = false; 私人可绘制的;私人油漆准确度油漆;私人点中心;私人点左; 私有 int 宽度;私人 int 高度;
public FixedMyLocationOverlay(Context context, MapView mapView) { super(context, mapView); } @Override protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) { if(!bugged) { try { super.drawMyLocation(canvas, mapView, lastFix, myLocation, when); } catch (Exception e) { // we found a buggy phone, draw the location icons ourselves bugged = true; } } if(bugged) { if(drawable == null) { accuracyPaint = new Paint(); accuracyPaint.setAntiAlias(true); accuracyPaint.setStrokeWidth(2.0f); drawable = mapView.getContext().getResources().getDrawable(R.drawable.here); width = drawable.getIntrinsicWidth(); height = drawable.getIntrinsicHeight(); center = new Point(); left = new Point(); } Projection projection = mapView.getProjection(); double latitude = lastFix.getLatitude(); double longitude = lastFix.getLongitude(); float accuracy = lastFix.getAccuracy(); float[] result = new float[1]; Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result); float longitudeLineDistance = result[0]; GeoPoint leftGeo = new GeoPoint((int)(latitude*1e6), (int)((longitude-accuracy/longitudeLineDistance)*1e6)); projection.toPixels(leftGeo, left); projection.toPixels(myLocation, center); int radius = center.x - left.x; accuracyPaint.setColor(0xff6666ff); accuracyPaint.setStyle(Style.STROKE); canvas.drawCircle(center.x, center.y, radius, accuracyPaint); accuracyPaint.setColor(0x186666ff); accuracyPaint.setStyle(Style.FILL); canvas.drawCircle(center.x, center.y, radius, accuracyPaint); drawable.setBounds(center.x - width/2, center.y - height/2, center.x + width/2, center.y + height/2); drawable.draw(canvas); } } }
代码活动
公共类 atm_atmogan 扩展 MapActivity {
private MapView mapView; private MyLocationOverlay myLocationOverlay; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.atm_atmogan); mapView = (MapView) findViewById(R.id.map_view); mapView.setBuiltInZoomControls(true); myLocationOverlay = new FixedMyLocationOverlay(this, mapView); mapView.getOverlays().add(myLocationOverlay); mapView.postInvalidate(); zoomToMyLocation(); } @Override protected void onResume() { super.onResume(); myLocationOverlay.enableMyLocation(); } @Override protected void onPause() { super.onPause(); myLocationOverlay.disableMyLocation(); } private void zoomToMyLocation() { GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation(); if(myLocationGeoPoint != null) { mapView.getController().animateTo(myLocationGeoPoint); mapView.getController().setZoom(18); } else { Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show(); } } @Override protected boolean isRouteDisplayed() { return false; } }