我在地图视图的帮助下制作了一张地图。我的地图正在运行,它也让我可以制作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;
}
}
答案 0 :(得分: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());
}
});
这将解决您的问题