我想在谷歌地图中打开我当前的位置

时间:2011-08-01 12:02:05

标签: android focus location

我想在Android中的谷歌地图中打开或关注我当前的位置。我希望我的应用程序获取坐标,然后从这些坐标中给出我的位置名称,最后聚焦当前位置图... 请有人建议我做什么以及如何做,如果可能的话,给我一些代码示例。我刚接触到android :) 提前致谢! :)

1 个答案:

答案 0 :(得分:4)

这是带有当前位置的简单地图显示示例

MyMap.java用于显示地图

class MyMap extends MapActivity{
/** Called when the activity is first created. */
GeoPoint defaultPoint;
static GeoPoint point;
static MapController mc;
static MapView mapView;
static double curLat =0;
static double curLng =0;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView)findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    String coordinates[] = {"22.286595", "70.795685"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    mc = mapView.getController();

    defaultPoint = new GeoPoint(
        (int) (lat * 1E6), 
        (int) (lng * 1E6));

    mc.animateTo(defaultPoint);
    mc.setZoom(13); 

    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);   

    mapView.invalidate();

    Intent startService = new Intent(getApplicationContext(),ServiceLocation.class);
    startService(startService);
}

public static void updateMap() {
    if(ServiceLocation.curLocation!=null){
        curLat = ServiceLocation.curLocation.getLatitude();
        curLng = ServiceLocation.curLocation.getLongitude();        
        if(mapView!=null){
            point = new GeoPoint((int)(curLat*1e6),(int)(curLng*1e6));
            mc.animateTo(point);
            mapView.invalidate();
        }
    }
}
@Override
protected boolean isRouteDisplayed(){
    return false;
}
class MapOverlay extends com.google.android.maps.Overlay{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);                   

        if(!shadow){
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            if(curLat==0 && curLng==0)
                mapView.getProjection().toPixels(defaultPoint, screenPts);
            else{
                mapView.getProjection().toPixels(point, screenPts);
            }
            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.cur_loc);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
        }
        return true;
    }
}
}

这是我使用gps获取lat / lng的后台服务代码

class ServiceLocation extends Service{
private LocationManager locMan;
private Boolean locationChanged;
private Handler handler = new Handler();

public static Location centerLoc;
public static Location curLocation;
public static boolean isService = true;


LocationListener gpsListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        if (curLocation == null) {
            curLocation = location;
            locationChanged = true;
        }else if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude()){
            locationChanged = false;
            return;
        }else
            locationChanged = true;

        curLocation = location;


        if (locationChanged)
            locMan.removeUpdates(gpsListener);


    MyMap.updateMap();
    }
    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
        // Log.w("GPS", "Location changed", null);
    }

    public void onStatusChanged(String provider, int status,Bundle extras) {
        if (status == 0)// UnAvailable
        {
        } else if (status == 1)// Trying to Connect
        {
        } else if (status == 2) {// Available
        }
    }

};

@Override
public void onCreate() {
    super.onCreate();
    if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
       locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,100, 1, gpsListener);
    } else {
       this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
    }
    centerLoc = new Location("");
    curLocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);;*/
    centerLoc = new Location("");
    curLocation = getBestLocation();

    if (curLocation == null) 
        Toast.makeText(getBaseContext(),"Unable to get your location", Toast.LENGTH_SHORT).show();

    isService =  true;
}
final String TAG="LocationService";
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
 return super.onStartCommand(intent, flags, startId);
   }



@Override
   public void onLowMemory() {
       super.onLowMemory();
   }

   @Override
   public void onStart(Intent i, int startId){
      handler.postDelayed(GpsFinder,5000);// will start after 5 seconds
   }

   @Override
   public void onDestroy() {
       handler.removeCallbacks(GpsFinder);
       handler = null;
       Toast.makeText(this, "Stop services", Toast.LENGTH_SHORT).show();
       isService = false;
   }

   public IBinder onBind(Intent arg0) {
         return null;
  }

  public Runnable GpsFinder = new Runnable(){
    public void run(){
        Location tempLoc = getBestLocation();
        if(tempLoc!=null)
            curLocation = tempLoc;
        handler.postDelayed(GpsFinder,5000);// register again to start after 5 seconds...        
    }
  };

    private Location getBestLocation() {        
        Location gpslocation = null;
        Location networkLocation = null;

        if(locMan==null)
              locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE);
        try {
            if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,100, 1, gpsListener);
                gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
            if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,100, 1, gpsListener);
                networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
            }
        } catch (IllegalArgumentException e) {
            //Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString());
            Log.e("error", e.toString());

        }
        if(gpslocation==null && networkLocation==null)
            return null;

        if(gpslocation!=null && networkLocation!=null){
            if(gpslocation.getTime() < networkLocation.getTime())
                return networkLocation;
            else
                return gpslocation;
        }
        if (gpslocation == null) {
            return networkLocation;
        }
        if (networkLocation == null) {
            return gpslocation;
        }
        return null;
    }
}
相关问题