2014-04-02 70 views
0

我需要在android中查找用户位置。由于我不想使用GPS,我尝试了下面的代码来根据他/她的网络查找用户位置,这在一些设备上可用,但不是所有设备都可以使用,问题是什么?是Android版本的问题还是硬件问题?Android位置监听器在某些设备上不起作用

private double x, y; 
private LocationManager lm; 
lm = (LocationManager) getActivity().getSystemService(
        Context.LOCATION_SERVICE); 
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 
        0, locationListenerNetwork); 


LocationListener locationListenerNetwork = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     x = location.getLatitude(); 
     y = location.getLongitude(); 
     lm.removeUpdates(this); 

     Geocoder gcd = new Geocoder(getActivity(), Locale.getDefault()); 
     List<Address> addresses; 
     addresses = gcd.getFromLocation(x, y, 1); 
     city = addresses.get(0).getLocality(); 

    } 

回答

0

尝试在FetchLocationTask类此 给予纬度和经度

latitude = 27.003434; 
    longitude = 23.458569; 
    new FetchLocationTask(getActivity(), latitude, longitude).execute(); 



    public class FetchLocationTask extends AsyncTask<Void, Void, Void> { 

      public Context context; 
      // private ProgressDialog mProgressDialog; 
      public String addressUrl; 
      public BufferedReader in; 

      public FetchLocationTask(Context context, double lat, double longi) { 
       this.context = context; 
       addressUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + longi + "&sensor=true"; 
      } 

      @Override 
      protected void onPreExecute() { 

      } 

      /** 
      * Call the webservice and parse the data from the service in 
      * background. 
      */ 
      @Override 
      protected Void doInBackground(Void... params) { 
       try { 
        HttpClient httpClient = new DefaultHttpClient();// Client 

        HttpGet getRequest = new HttpGet(); 
        getRequest.setURI(new URI(addressUrl)); 

        HttpResponse response = httpClient.execute(getRequest); 
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
        StringBuffer sb = new StringBuffer(""); 
        String line = ""; 
        String NL = System.getProperty("line.separator"); 
        while ((line = in.readLine()) != null) { 
         sb.append(line + NL); 
        } 
        in.close(); 
        String page = sb.toString(); 

        JSONObject jsonObject = new JSONObject(page); 

        if (jsonObject.has("results")) { 
         JSONArray jsonArray = (JSONArray) jsonObject.get("results"); 

         if (jsonArray.length() > 0) { 
          jsonObject = (JSONObject) jsonArray.get(0); 
          if (jsonObject.has("formatted_address")) { 
           address = (String) jsonObject.get("formatted_address"); 
          } 
         } 
         return null; 
        } 
        return null; 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } finally { 
        if (in != null) { 
         try { 
          in.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
       return null; 
      } 

      /** 
      * onPostExecute method after called webService and Set the data into 
      * adapter after background task is complete. 
      */ 
      @Override 
      protected void onPostExecute(Void result) { 

       if (address != null && address.length() > 0) { 

        Log.i("map", "My:::" + address); 
        etx_address.setText(address.toString()); 

       } 
      } 
     } 
相关问题