在室内获得gps坐标

时间:2011-08-05 12:48:14

标签: android geolocation gps

这是我的代码,我似乎无法在使用NETWORK_PROVIDER时获取坐标。 onLocationChange()似乎没有激发。我将非常感谢你的帮助。

package com.networkprovider;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

public class networkProvider extends Activity {
    EditText editText1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editText1=(EditText)findViewById(R.id.editText1);
    }
    @Override
    protected void onStart() {
        LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener listener=new LocationListener() {

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

            }

            @Override
            public void onProviderEnabled(String arg0) {


            }
            @Override
            public void onProviderDisabled(String arg0) {
            }

            @Override
            public void onLocationChanged(Location location) {
                String loc=String.valueOf(location.getLatitude())+","+String.valueOf(location.getLongitude());
                Toast.makeText(getApplicationContext(),loc, Toast.LENGTH_LONG).show();
                //editText1.setText(loc);

            }
        };
        boolean isEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if(isEnabled)
        {
            Toast.makeText(getApplicationContext(),"Enabled", Toast.LENGTH_LONG).show();
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
        }
        else
        {
            Toast.makeText(getApplicationContext(),"Not Enabled!", Toast.LENGTH_LONG).show();
        }
        super.onStart();
    }

}

2 个答案:

答案 0 :(得分:0)

我运行了这段代码,它让我在吐司中找到了我的位置

检查你有

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
清单中的

或设备已打开网络位置:)

答案 1 :(得分:0)

这可能是一个愚蠢的答案,但你确定它应该被触发吗?

只要您呆在室内,由于网络的精度非常低,也许您无法移动足够触发onLocationChange()。在我家,网络精度大约是1km,所以你必须得到一个非常大的建筑才能使用这种数据进行本地化......

安托