2011-06-05 183 views
0
i just need to find the current location on maps using WIFI.I used tha below code to do that. 

我的代碼:當前位置的Android

公共類LocationActivity擴展MapActivity實現LocationListener的{

private MapView mapView; 
private LocationManager locationManager; 
private String latitude,longtitude; 

@Override 
protected boolean isRouteDisplayed() { 
    return false; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    Bundle bundle = this.getIntent().getExtras(); 
    latitude = bundle.getString("latitude"); 
    longtitude = bundle.getString("longtitude"); 
    setContentView(R.layout.locationtab); 

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

} 




private void createMyLocOverlay(Double latitude, Double longtitude) { 

    List<Overlay> mapOverlays = mapView.getOverlays(); 
    Drawable drawable = this.getResources().getDrawable(
      R.drawable.mylocation); 

    GeoPoint point = new GeoPoint((int) (latitude * 1E6), 
      (int) (longtitude * 1E6)); 
    OverlayItem overlayitem = new OverlayItem(point, null, "You are here!"); 
    MyLocationOverlay itemizedoverlay = new MyLocationOverlay(drawable, 
      this); 
    itemizedoverlay.addOverlay(overlayitem); 
    MyLocationOverlay overlayToRemove = null; 
    for (Overlay overlay : mapOverlays) { 
     if (overlay instanceof MyLocationOverlay) { 
      overlayToRemove = (MyLocationOverlay) overlay; 
     } 
    } 
    if (overlayToRemove != null) { 
     mapOverlays.remove(overlayToRemove); 
    } 
    mapOverlays.add(itemizedoverlay); 
} 

public void locationIdentifier() { 


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

    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 location = locationManager.getLastKnownLocation(provider); 

    if (location != null) { 
     createMyLocOverlay(location.getLatitude(), location.getLongitude()); 
     Toast.makeText(
       this, 
       "Latitude : " + location.getLatitude() + " : Longtitude : " 
         + location.getLongitude(), Toast.LENGTH_LONG) 
       .show(); 
    } else { 
     Toast.makeText(this, "Latitude/Longtitude not found", 
       Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    if (location != null) { 
     createMyLocOverlay(location.getLatitude(), location.getLongitude()); 
     Toast.makeText(
       this, 
       "LocationChanged Latitude : " + location.getLatitude() 
         + " Longtitude : " + location.getLongitude(), 
       Toast.LENGTH_LONG).show(); 
    } else { 
     Toast.makeText(this, "Location is null", Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void onProviderDisabled(String provider) { 
    Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_LONG) 
      .show(); 

} 

@Override 
public void onProviderEnabled(String provider) { 
    Toast.makeText(this, "Enabled new provider " + provider, 
      Toast.LENGTH_LONG).show(); 
} 

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

}

我怎麼會檢查它在仿真器是否其工作與否。?

回答

0

現在,不可能使用模擬器來模擬它,因爲它根本不支持它。或者,你可以設置一個標誌常量(例如DEBUG = false/true),如果DEBUG = true,則使用常量位置,否則使用WIFI位置。

或使用可支持IP地址位置的位置提供程序。如果DEBUG = true,您可以將其用作備選項。