在地图中定位我的位置的问题
Posted
技术标签:
【中文标题】在地图中定位我的位置的问题【英文标题】:Issue with locating my location in map 【发布时间】:2012-02-15 13:48:12 【问题描述】:我在地图视图的帮助下制作了一张地图。我的地图正在工作,它还允许我制作 maap 的屏幕截图。但我无法找到我当前的位置。我不知道为什么我的代码显示在这里。谁能告诉我如何找到我当前的位置。
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
0,
0,
locationListener);
// Location intial = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// GeoPoint mainone=new GeoPoint((int) (intial.getLatitude() * 1E6),
// (int) (intial.getLongitude() * 1E6));
//
// mapController.animateTo(mainone);
mapView = (MapView) findViewById(R.id.mapView);
capture=(Button) findViewById(R.id.bcapture);
capture.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
getMapImage();
saveMapImage();
);
// enable Street view by default
mapView.setStreetView(false);
// enable to show Satellite view
// mapView.setSatellite(true);
// enable to show Traffic on map
// mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
// mapController.setZoom(16);
private Bitmap getMapImage()
/* Position map for output */
MapController mc = mapView.getController();
// mc.setCenter(SOME_POINT);
// mc.setZoom(16);
/* Capture drawing cache as bitmap */
mapView.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(mapView.getDrawingCache());
mapView.setDrawingCacheEnabled(false);
return bmp;
private void saveMapImage()
String filename = "foo.png";
File f = new File("/sdcard/", filename);
FileOutputStream out = null;
try
out = new FileOutputStream(f);
catch (FileNotFoundException e)
e.printStackTrace();
Bitmap bmp = getMapImage();
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
try
out.close();
catch (IOException e)
e.printStackTrace();
@Override
protected boolean isRouteDisplayed()
return false;
private class GPSLocationListener implements LocationListener
@Override
public void onLocationChanged(Location location)
Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location1 != null)
GeoPoint point = new GeoPoint(
(int) (location1.getLatitude() * 1E6),
(int) (location1.getLongitude() * 1E6));
/* Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();*/
System.out.println(point.getLatitudeE6()+point.getLongitudeE6()+"helooooooo");
System.out.println(point.getLatitudeE6()+"buhahahooo");
mapController.animateTo(point);
// mapController.setZoom(16);
// add marker
MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(point);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
mapView.invalidate();
public String ConvertPointToLocation(GeoPoint point)
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0)
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
catch (IOException e)
e.printStackTrace();
return address;
@Override
public void onProviderDisabled(String provider)
@Override
public void onProviderEnabled(String provider)
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
class MapOverlay extends Overlay
private GeoPoint pointToDraw;
public void setPointToDraw(GeoPoint point)
pointToDraw = point;
public GeoPoint getPointToDraw(
)
return pointToDraw;
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
super.draw(canvas, mapView, shadow);
// convert point to pixels
Point screenPts = new Point();
mapView.getProjection().toPixels(pointToDraw, screenPts);
// add marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image
return true;
【问题讨论】:
【参考方案1】:尝试使用 MyLocationOverlay 类的对象
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverLay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable()
public void run()
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
);
这会解决你的问题
【讨论】:
以上是关于在地图中定位我的位置的问题的主要内容,如果未能解决你的问题,请参考以下文章