2011-08-05 46 views
0

这是我的代码,我似乎无法获得使用NETWORK_PROVIDER时的坐标。 onLocationChange()似乎不会触发。我会感激的帮助。在室内时获得GPS坐标

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(); 
    } 

} 
+0

你的代码有没有问题,只是检查权限位置也适用于互联网 – Dharmendra

回答

0

我跑这个代码,并在祝酒回到了我的位置

检查是否具有

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

,或者该设备具有网络位置打开:)

+0

是的,我在清单中有。通过网络位置你的意思是“使用无线网络”? – John

+0

使用无线网络会带来更好的结果:)编辑:事实上,如果你的意思是这个选项,那么你需要的是yes编辑2:如果你打开了wifi,它会给出更准确的结果(原本我是说) – dten

0

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

只要你留在室内,由于网络的精度非常低,也许你不能动起来触发onLocationChange()。在我的家乡,网络精度为1公里,所以你必须得到一个非常大的建筑使用这种数据做本地化的...

安东尼

+0

它真的很无聊 –