本文介绍了在没有GPS的情况下在android中定位当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个应用程序,如果我打开该应用程序,它将显示我设备的当前位置.我想不使用gps做到这一点.我已经为此编写了代码.但是在打开地图时它将显示美国的位置.我希望它显示我的当前位置(即印度的浦那).如何获得我的大致正确位置?

I want to develop an application in which, if i open the app, it will show current location of my device.I want to do it without using gps. I have write a code for it. But it will show United States location while opening the map. I want it to show my current location (i.e Pune, India).How do i get my approximate correct location?

这是我的代码

protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.main);

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

      LocationListener locListener = new LocationListener(){
          public void onLocationChanged(Location loc)
          {

            loc.getLatitude();
            loc.getLongitude();

            String Text = "My current location is: " +
            "Latitud = " + loc.getLatitude() +
            "Longitud = " + loc.getLongitude();

            Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
          }

推荐答案

您应该请求' mlocManager '进行定位,并设置其侦听器:

You should request 'mlocManager' to locate, and set its listener:

public void getCurrentLocation()
{
    if (mlocManager != null) {
        mlocManager .requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locListener );
        mlocManager .requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locListener );
    }

}

这篇关于在没有GPS的情况下在android中定位当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:39