2014-04-02 6 views
0

Android 위치에서 사용자 위치를 찾아야합니다. GPS를 사용하고 싶지 않기 때문에 네트워크를 기반으로 사용자 위치를 찾기 위해 아래 코드를 시도했지만 일부 장치에서 작동하지만 모든 장치에서 작동하지 않습니다. 무엇이 문제입니까? 그것은 안드로이드 버전이나 하드웨어 문제 같은 것이기 때문입니까?일부 기기에서는 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()); 

       } 
      } 
     } 
관련 문제