开发者

getting gps coordinates while indoors

Here is my code, i cant seem to get the coordinates while using NETWORK_PROVIDER. onLocationChange() does not seem to fire. I will appreciate the help.

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(Bundl开发者_如何学JAVAe 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();
    }

}


I ran this code and it returned me my location in a toast

check that you have

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

in your manifest or that the device has network locations turned on :)


It may be a silly answer, but are you sure it Should be triggered?

As long as you stay indoors, as the precision of the network is really low, maybe you can't move enough to trigger onLocationChange(). At my home, network precision is about 1km, so you have to get a really big building to use this kind of data to do localisation...

Antoine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜