在android中使用WIFI在地图上显示当前位置

发布于 2024-11-13 23:50:25 字数 3641 浏览 1 评论 0原文

i just need to find the current location on maps using WIFI.I used tha below code to do that.

我的代码:

public class LocationActivity extends MapActivity Implements 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) {
}

}

我如何在模拟器上检查它是否正常工作。

i just need to find the current location on maps using WIFI.I used tha below code to do that.

My code:

public class LocationActivity extends MapActivity implements 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) {
}

}

How could i check it on emulator whether its working or not.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2024-11-20 23:50:25

目前,无法使用模拟器来模拟它,因为它根本不支持它。作为替代方案,您可以放置​​一个标志常量(例如 DEBUG=false/true),然后如果 DEBUG=true,则使用常量位置,否则使用 WIFI 位置。

或者使用可以支持IP地址定位的定位提供商。如果 DEBUG=true,您可以使用它作为替代方案。

For now, It is not possible to simulate it using emulator because it is simply doesn't support it. As alternative, you may put a flag constant (e.g. DEBUG=false/true) then if DEBUG=true, use the constant location otherwise use WIFI location.

Or use location provider which can support IP Address location. Which you can use as alternative if DEBUG=true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文