2011-03-10 61 views
1

在这里,我创建一个项目来跟踪我们当前的位置。 但是我有一个编码问题......我不清楚如何使用OVERLAY类以及何时调用它。我想跟踪我在Android应用程序中的当前位置

我已经发布我的代码。请帮我

package com.techno.gps; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.Projection; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.graphics.RectF; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

public class gps extends MapActivity{ 

    MapController mapController; 
    Location location; 
    MyPositionOverlay positionOverlay; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    // Get a reference to the MapView 
     MapView myMapView = (MapView)findViewById(R.id.mapView); 
     // Get the Map View’s controller 
     mapController = myMapView.getController(); 
     // Configure the map display options 
     myMapView.setSatellite(true); 
     myMapView.setStreetView(true); 
     myMapView.displayZoomControls(false); 
     // Zoom in 
     mapController.setZoom(17); 
     //add postionoverlay 
     positionOverlay = new MyPositionOverlay(); 
     List<Overlay> overlays = myMapView.getOverlays(); 
     overlays.add(positionOverlay); 

     LocationManager locationManager; 
     String context = Context.LOCATION_SERVICE; 
     locationManager = (LocationManager)getSystemService(context); 

     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(true); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 

     String provider = locationManager.getBestProvider(criteria, true); 
     location = locationManager.getLastKnownLocation(provider); 
     updateWithNewLocation(location); 
     locationManager.requestLocationUpdates(provider, 2000, 10,locationListener); 

    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    private void updateWithNewLocation(Location location) 
    { 
     String latLongString; 
     TextView myLocationText; 
     myLocationText = (TextView)findViewById(R.id.myLocationText); 

     String addressString = "No address found"; 
     if (location != null) { 
     // Update the map location. 
     positionOverlay.setLocation(location); 
     Double geoLat = location.getLatitude()*1E6; 
     Double geoLng = location.getLongitude()*1E6; 
     GeoPoint point = new GeoPoint(geoLat.intValue(), 
     geoLng.intValue()); 
     mapController.animateTo(point); 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     latLongString = "Lat: "+ lat + " Long:" + lng; 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
     Geocoder gc = new Geocoder(this, Locale.getDefault()); 
     try 
     { 
       List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); 
       StringBuilder sb = new StringBuilder(); 
       if (addresses.size() > 0) 
       { 
        Address address = addresses.get(0); 
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++) 
        { 
         sb.append(address.getAddressLine(i)).append("\n"); 
        } 
        sb.append(address.getLocality()).append("\n"); 
        sb.append(address.getPostalCode()).append("\n"); 
        sb.append(address.getCountryName()); 
       } 
       addressString = sb.toString(); 
       } 
       catch (IOException e) 
       { 

       } 

     } 
      else 
      { 
       latLongString = "No location found"; 
      } 
      myLocationText.setText("Your Current Position is:\n" +latLongString + "\n" + addressString); 


     } 

     private final LocationListener locationListener = new LocationListener() 
     { 
       public void onLocationChanged(Location location) { 
       updateWithNewLocation(location); 
     } 
     public void onProviderDisabled(String provider) 
     { 
      updateWithNewLocation(null); 
     } 
     public void onProviderEnabled(String provider) 
     { 

     } 
     public void onStatusChanged(String provider, int status,Bundle extras) 
     { 

     } 
    }; 

    public class MyPositionOverlay extends Overlay 
    { 
     Location location; 
     @Override 
     public void draw(Canvas canvas, MapView mapView, boolean shadow) 
     { 
      Projection projection = mapView.getProjection(); 
      if (shadow == false) { 
      // Get the current location 
      Double latitude = location.getLatitude()*1E6; 
      Double longitude = location.getLongitude()*1E6; 
      GeoPoint geoPoint; 
      geoPoint = new GeoPoint(latitude.intValue(),longitude.intValue()); 
      // Convert the location to screen pixels 
      int mRadius=5; 
      Point point = new Point(); 
      projection.toPixels(geoPoint, point); 
      RectF oval = new RectF(point.x - mRadius, point.y - mRadius, 
      point.x + mRadius, point.y + mRadius); 
      // Setup the paint 
      Paint paint = new Paint(); 
      paint.setARGB(250, 255, 0, 0); 
      paint.setAntiAlias(true); 
      paint.setFakeBoldText(true); 
      Paint backPaint = new Paint(); 
      backPaint.setARGB(175, 50, 50, 50); 
      backPaint.setAntiAlias(true); 
      RectF backRect = new RectF(point.x + 2 + mRadius, 
      point.y - 3*mRadius, 
      point.x + 65, point.y + mRadius); 
      // Draw the marker 
      canvas.drawOval(oval, paint); 
      canvas.drawRoundRect(backRect, 5, 5, backPaint); 
      canvas.drawText("Here I Am", point.x + 2*mRadius, point.y, paint); 
      } 
      super.draw(canvas, mapView, shadow); 
     } 
     @Override 
     public boolean onTap(GeoPoint point, MapView mapView) 
     { 
      // Return true if screen tap is handled by this overlay 
      return false; 
     } 

     public Location getLocation() { 
     return location; 
     } 
     public void setLocation(Location location) { 
     this.location = location; 
     } 

    } 

} 

感谢...

回答

2

奇怪的是,你不知道自己的代码。你应该尝试自己写。复制粘贴不会始终工作。

回答你的问题。覆盖图是可绘制的对象,可以在地图顶部显示在MapView上方的不同图层上。

这是将图形添加到MapView的代码的一部分。

positionOverlay = new MyPositionOverlay(); 
    List<Overlay> overlays = myMapView.getOverlays(); 
    overlays.add(positionOverlay); 

您正在OnCreate()中执行此操作。这是没有道理的,因为你还没有定位。

  1. 添加覆盖,当你在一个updateWithNewLocation POS修复()
  2. 要调用抽签强制使用MapView.invalidate()
1

我有同样的代码示例,以2.2的Android,工作,但不要在我的位置上覆盖覆盖层,否则,使用调试代码你可以看到代码正常工作。真奇怪。

我觉得代码是正确的,因为,在的onCreate,代码initializated,然后在updateWithNewLocation,覆盖与positionOverlay.setLocation(location);

更新
相关问题