2016-08-28 18 views
0

私はGPSなしで緯度と経度を取得しようとしていますが、それは私にnullを与えます。しかし、私は私のGPS上で、それは正しい緯度の値を与えます。私はwifiで緯度と経度を取得したい。これをどうすれば解決できますか?ネットワークプロバイダのコードは次のとおりです。LocationManager.NETWORK_PROVIDERが機能していません

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 
     // getting GPS status 
     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 
     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
      return null; 
     } else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 

       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        // TODO: Consider calling 
        // ActivityCompat#requestPermissions 
        // here to request the missing permissions, and then overriding 
        // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
        //           int[] grantResults) 
        // to handle the case where the user grants the permission. See the documentation 
        // for ActivityCompat#requestPermissions for more details. 
        return null; 
       } 
       //updates will be send according to these arguments 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
         // TODO: Consider calling 
         // ActivityCompat#requestPermissions 
         // here to request the missing permissions, and then overriding 
         // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
         //           int[] grantResults) 
         // to handle the case where the user grants the permission. See the documentation 
         // for ActivityCompat#requestPermissions for more details. 
         return null; 
        } 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS Enabled", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return location; 
} 
+0

デバイスがネットワークとGPSの間で最適なオプションを選択できるようにするのは良いことですが、Googleの最新のFusedLocation APIも使用できますか? – Eenvincible

+0

あなたはfusedLocation APIのいくつかの例を挙げることができますか?私はアンドロイドで新しいです – Taimoor

+1

@ Taimor [documentation](https://developer.android.com/training/location/receive-location-updates.html)全体は段階的です。あなたはそれを読みました? –

答えて

1

WifiManagerクラスを試してみてください。文書で述べたように

このクラスは、Wi-Fi接続のすべての側面を管理するための主要なAPIを提供します。 Context.getSystemService(Context.WIFI_SERVICE)を呼び出して、このクラスのインスタンスを取得します。

Wi-Fi固有の操作を実行するときに使用するAPIです。

また、この記事の後半のAndroid: How to Enable/Disable Wifi or Internet Connection Programmaticallyで解決策を確認してください。それが役に立てば幸い!

+0

おかげで君はやってみるよ – Taimoor

0

ネットワークプロバイダが有効になっているかどうかを問い合わせます。 locationManager.isProviderEnabledあなたが使用して位置情報の更新を要求することができます

(LocationManager.NETWORK_PROVIDER):network_providerを使用して有効になっているかどうかをチェックすることができます locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER、0、0、これを)。

最後に既知の場所を取得するには、次のようにします。 場所lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

私はgithub repoで動作していたサンプルアプリケーションを持っています。しかし最近の更新はありません。リンクは次のとおりです。https://github.com/sauravpradhan/TrackMe-Ver-2.0

これが役立つ場合は、戻ってください。

+0

助けてくれてありがとう – Taimoor

+0

緯度経度と – Taimoor

0

提供されているすべての情報があります。しかし、LatとLongから位置を取得するには、GeoCoderを使用する必要があります。

Geocoder g1= new Geocoder(getApplicationContext()); 
     List<Address> locale= null; 
     String add = null; 
     try 
     { 
      locale = g1.getFromLocation(location.getLatitude(), location.getLongitude(), 3); 
     } 
     catch(Exception e) 
     { 
     Log.d("Saurav_log", "Cant fetch address"); 
     } 
    /* finally 
     { 
     Log.d("Saurav_log", "Finally Exiting"); 
     }*/ 
      TextView textView2 = (TextView)findViewById(R.id.textView2); 
      if(locale != null) 
      { 
       add =locale.get(0).getAddressLine(0); 
      } 
textView2.setText("From GPS:Actual Address \n"+add+locale.get(0).getLocality()+" \n"+locale.get(0).getCountryName()+" \n"+locale.get(0).getSubLocality()); 
+0

助けのこの種のおかげで男 – Taimoor

+0

あなたの目的を解決する場合は、答えを受け入れます。 – Saurav

関連する問題