2012-01-21 4 views
2

지도보기를 사용하여지도를 만들었습니다. 내지도가 작동하며지도의 스크린 샷을 만들 수도 있습니다. 그러나 현재 위치를 찾을 수 없습니다. .i 내 코드가 여기에 표시되는 이유를 알 수 없습니다. 현재 위치를 찾을 수있는 방법을 누군가가 알 수 있습니다.지도에서 내 위치를 찾을 때의 문제

 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 = new MyLocationOverlay(this,mapView); 
mapView.getOverlays().add(myLocationOverlay); 
myLocationOverLay.enableMyLocation(); 
myLocationOverlay.runOnFirstFix(new Runnable(){ 
    public void run(){ 
     mapView.getController().animateTo(myLocationOverlay.getMyLocation()); 
    } 
}); 

이 문제를 해결할 수 MyLocationOverlay 클래스의 객체를 사용하는

관련 문제