2016-11-01 1 views
0

Ich habe gerade die Berechtigungen über die Bibliothek abgewickelt Lassen Sie, aber ich kann immer noch nicht einen Längen- und Breitengrad in meiner Android App bekommen. Ich benutze einen Service namens GPS_Service.java. Ich bin auch nicht 100% sicher, dass meine Berechtigungen nicht das Problem sind. Der Aufruf der Google API in GPS_Service ist immer noch mit der Warnung unterstrichen, dass Berechtigungen erforderlich sind, die möglicherweise abgelehnt werden. Soll das auch mit Bibliotheken von Drittanbietern weggehen? Wäre das immer noch ein Problem, weil auf meinem Gerät die Standortberechtigungen aktiviert sind?Breiten- und Längengrad geben immer 0.0 zurück, wenn lastKnownLocation verwendet wird

hier ist meine Haupttätigkeit:

package com.example.paxie.stormy.ui; 

import android.Manifest; 
import android.annotation.TargetApi; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.pm.PackageManager; 
import android.graphics.drawable.Drawable; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.Build; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.support.annotation.NonNull; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.canelmas.let.AskPermission; 
import com.canelmas.let.DeniedPermission; 
import com.canelmas.let.Let; 
import com.canelmas.let.RuntimePermissionListener; 
import com.canelmas.let.RuntimePermissionRequest; 
import com.example.paxie.stormy.GPS_Service; 
import com.example.paxie.stormy.R; 
import com.example.paxie.stormy.weather.Current; 
import com.example.paxie.stormy.weather.Day; 
import com.example.paxie.stormy.weather.Forecast; 
import com.example.paxie.stormy.weather.Hour; 
import com.google.android.gms.maps.model.LatLng; 
import com.squareup.okhttp.Call; 
import com.squareup.okhttp.Callback; 
import com.squareup.okhttp.OkHttpClient; 
import com.squareup.okhttp.Request; 
import com.squareup.okhttp.Response; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 
import java.util.List; 

import butterknife.BindView; 
import butterknife.ButterKnife; 
import butterknife.OnClick; 

public class MainActivity extends AppCompatActivity implements RuntimePermissionListener { 

    public static final String TAG = MainActivity.class.getSimpleName(); 
    public static final String DAILY_FORECAST = "DAILY_FORECAST"; 
    public static final String HOURLY_FORECAST = "HOURLY_FORECAST"; 
    private Forecast mForecast; 
    private double mLatitude; 
    private double mLongitude; 
    private BroadcastReceiver broadcastReceiver; 
    private Location location; // location 
    private double latitude; // latitude 
    private double longitude; // longitude 
    private Context mContext; 

    @BindView(R.id.timeLabel) 
    TextView mTimeLabel; 
    @BindView(R.id.temperatureLabel) 
    TextView mTemperatureLabel; 
    @BindView(R.id.humidityValue) 
    TextView mHumidityValue; 
    @BindView(R.id.precipValue) 
    TextView mPrecipValue; 
    @BindView(R.id.summaryLabel) 
    TextView mSummaryLabel; 
    @BindView(R.id.iconImageView) 
    ImageView mIconImageView; 
    @BindView(R.id.refreshImageView) 
    ImageView mRefreshImageView; 
    @BindView(R.id.progressBar) 
    ProgressBar mProgressBar; 
    @BindView(R.id.locationLabel) 
    TextView mLocationlabel; 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     Let.handle(this, requestCode, permissions, grantResults); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ButterKnife.bind(this); 


     mProgressBar.setVisibility(View.INVISIBLE); 

     mRefreshImageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       getForecast(); 
      } 
     }); 


     getForecast(); 
     Log.d(TAG, "Main UI code is running!"); 
    } 

    private void getForecast() { 
     checkGPS(); 
     String apiKey = "1621390f8c36997cb1904914b726df52"; 
     String forecastUrl = "https://api.forecast.io/forecast/" + apiKey + 
       "/" + mLatitude + "," + mLongitude; 

     if (isNetworkAvailable()) { 
      toggleRefresh(); 

      OkHttpClient client = new OkHttpClient(); 
      Request request = new Request.Builder() 
        .url(forecastUrl) 
        .build(); 

      Call call = client.newCall(request); 
      call.enqueue(new Callback() { 
       @Override 
       public void onFailure(Request request, IOException e) { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          toggleRefresh(); 
         } 
        }); 
        alertUserAboutError(); 
       } 

       @Override 
       public void onResponse(Response response) throws IOException { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          toggleRefresh(); 
         } 
        }); 
        try { 
         String jsonData = response.body().string(); 
         Log.v(TAG, jsonData); 
         if (response.isSuccessful()) { 
          mForecast = parseForecastDetails(jsonData); 
          runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            updateDisplay(); 
           } 
          }); 

         } else { 
          alertUserAboutError(); 

         } 
        } catch (IOException e) 

        { 
         Log.e(TAG, "Exception caught: ", e); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
     } else { 
      Toast.makeText(this, "Network is currently unavailable!", Toast.LENGTH_LONG).show(); 
     } 
    } 


    private void toggleRefresh() { 
     if (mProgressBar.getVisibility() == View.INVISIBLE) { 
      mProgressBar.setVisibility(View.VISIBLE); 
      mRefreshImageView.setVisibility(View.INVISIBLE); 
     } else { 
      mProgressBar.setVisibility(View.INVISIBLE); 
      mRefreshImageView.setVisibility(View.VISIBLE); 
     } 
    } 

    private void updateDisplay() { 
     mLocationlabel.setText(mLatitude + " " + mLongitude); 
     Current current = mForecast.getCurrent(); 
     mTemperatureLabel.setText(current.getTemperature() + ""); 
     mTimeLabel.setText("At " + current.getFormattedTime() + " it will be:"); 
     mHumidityValue.setText(current.getHumidity() + ""); 
     mPrecipValue.setText(current.getPrecipChance() + "%"); 
     mSummaryLabel.setText(current.getSummary()); 
     Drawable drawable = ContextCompat.getDrawable(this, current.getIconId()); 
     mIconImageView.setImageDrawable(drawable); 
    } 

    private Forecast parseForecastDetails(String jsonData) throws JSONException { 
     Forecast forecast = new Forecast(); 
     forecast.setCurrent(getCurrentDetails(jsonData)); 
     forecast.setHourlyForecast(getHourlyForecast(jsonData)); 
     forecast.setDailyForecast(getDailyForecast(jsonData)); 

     return forecast; 

    } 

    private Day[] getDailyForecast(String jsonData) throws JSONException { 
     JSONObject forecast = new JSONObject(jsonData); 
     String timezone = forecast.getString("timezone"); 
     JSONObject daily = forecast.getJSONObject("daily"); 
     JSONArray data = daily.getJSONArray("data"); 

     Day[] days = new Day[data.length()]; 

     for (int i = 0; i < data.length(); i++) { 
      JSONObject jsonDay = data.getJSONObject(i); 
      Day day = new Day(); 

      day.setSummary(jsonDay.getString("summary")); 
      day.setIcon(jsonDay.getString("icon")); 
      day.setTemperatureMax(jsonDay.getDouble("temperatureMax")); 
      day.setTime(jsonDay.getLong("time")); 
      day.setTimeZone(timezone); 

      days[i] = day; 

     } 
     return days; 
    } 

    private Hour[] getHourlyForecast(String jsonData) throws JSONException { 
     JSONObject forecast = new JSONObject(jsonData); 
     String timezone = forecast.getString("timezone"); 
     JSONObject hourly = forecast.getJSONObject("hourly"); 
     JSONArray data = hourly.getJSONArray("data"); 

     Hour[] hours = new Hour[data.length()]; 

     for (int i = 0; i < data.length(); i++) { 
      JSONObject jsonHour = data.getJSONObject(i); 
      Hour hour = new Hour(); 
      hour.setSummary(jsonHour.getString("summary")); 
      hour.setTemperature(jsonHour.getDouble("temperature")); 
      hour.setIcon(jsonHour.getString("icon")); 
      hour.setTime(jsonHour.getLong("time")); 
      hour.setTimeZone(timezone); 

      hours[i] = hour; 

     } 
     return hours; 
    } 

    private Current getCurrentDetails(String jsonData) throws JSONException { 
     JSONObject forecast = new JSONObject(jsonData); 
     String timezone = forecast.getString("timezone"); 
     Log.i(TAG, "From JSON: " + timezone); 

     JSONObject currently = forecast.getJSONObject("currently"); 
     Current current = new Current(); 
     current.setHumidity(currently.getDouble("humidity")); 
     current.setTime(currently.getLong("time")); 
     current.setIcon(currently.getString("icon")); 
     current.setPrecipChance(currently.getDouble("precipProbability")); 
     current.setSummary(currently.getString("summary")); 
     current.setTemperature(currently.getDouble("temperature")); 
     current.setTimeZone(timezone); 

     Log.d(TAG, current.getFormattedTime()); 

     return current; 
    } 

    private boolean isNetworkAvailable() { 
     ConnectivityManager manager = (ConnectivityManager) 
       getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = manager.getActiveNetworkInfo(); 
     boolean isAvailable = false; 
     if (networkInfo != null && networkInfo.isConnected()) { 
      isAvailable = true; 
     } 
     return isAvailable; 
    } 


    private void alertUserAboutError() { 
     AlertDialogFragment dialog = new AlertDialogFragment(); 
     dialog.show(getFragmentManager(), "error_dialog"); 
    } 


    @OnClick(R.id.dailyButton) 
    public void startDailyActivity(View view) { 
     Intent intent = new Intent(this, DailyForecastActivity.class); 
     intent.putExtra(DAILY_FORECAST, mForecast.getDailyForecast()); 
     startActivity(intent); 

    } 

    @OnClick(R.id.hourlyButton) 
    public void startHourlyActivity(View view) { 
     Intent intent = new Intent(this, HourlyForecastActivity.class); 
     intent.putExtra(HOURLY_FORECAST, mForecast.getHourlyForecast()); 
     startActivity(intent); 
    } 
@AskPermission({Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}) 
    private void checkGPS() { 
     GPS_Service gps_service = new GPS_Service(this); 
     gps_service.getLocation(); 
     mLatitude = gps_service.getLatitude(); 
     mLongitude = gps_service.getLongitude(); 
    } 

    @Override 
    public void onShowPermissionRationale(List<String> permissionList, RuntimePermissionRequest permissionRequest) { 

    } 

    @Override 
    public void onPermissionDenied(List<DeniedPermission> deniedPermissionList) { 
    } 
} 

Und hier ist mein GPS_Service.java:

package com.example.paxie.stormy; 

import android.*; 
import android.Manifest; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.provider.Settings; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 

import com.canelmas.let.AskPermission; 
import com.canelmas.let.DeniedPermission; 
import com.canelmas.let.Let; 
import com.canelmas.let.RuntimePermissionListener; 
import com.canelmas.let.RuntimePermissionRequest; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationServices; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by paxie on 10/27/16. 
*/ 

public class GPS_Service extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

    private Location location; // location 
    private double latitude; // latitude 
    private double longitude; // longitude 
    private GoogleApiClient mGAC; 
    private Context mContext; 
    public static final String TAG = "GPSresource"; 
    private static final int RC_GPS_PERMS = 124; 
    public String[] perm = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}; 

    public GPS_Service(Context c) { 
     mContext = c; 
     try { 
      buildGoogleApiClient(); 
      mGAC.connect(); 
     } catch (Exception e) { 
      Log.d(TAG, e.toString()); 
     } 
    } 



    protected synchronized void buildGoogleApiClient() { 
     mGAC = new GoogleApiClient.Builder(mContext) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 


    public double getLatitude() { 
     if (location != null) { 
      latitude = location.getLatitude(); 
     } 

     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    */ 
    public double getLongitude() { 
     if (location != null) { 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 

    } 


    @Override 
    public void onConnectionSuspended(int i) { 

    } 


    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

    public void getLocation() { 
     location = LocationServices.FusedLocationApi.getLastLocation(mGAC); 
    } 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
} 

Antwort

0

In diesem Fall Ihr GPS aktivieren und gehen Google Maps und den Standort Knopf drücken, warten bis google maps dich finden und dann zurückgehen und in deiner app testen. Dies sollte funktionieren, denn wenn Sie das erste Mal Ihr Telefon verwenden, ohne jemals mit GPS verbunden zu sein, gibt es Ihnen das Problem

+0

keine Würfel. Es zeigt immer noch 0,0, 0,0 –

0

Es ist auf der Suche nach speziell "LastKnownLocation", hatte ich dieses Problem auch. Bewege dich ungefähr 3 Meter und versuche es erneut. Wenn es keinen "letzten bekannten Ort" gibt, wird ein Nullwert gesendet.

Was ich getan habe, war ein paar If-Anweisungen für meine Anwendung. Wenn der GPS-Provider NULL-Werte ausspuckt, dann verwenden Sie die Standortkoordinaten des Netzwerkanbieters, und wenn dieser NULL-Werte ausspuckt, dann habe ich nur die Meldung "Warten" oder "Erneut versuchen" angezeigt. Eventuell holt Ihr GPS-Anbieter nach einer gewissen Zeit die Standortwerte ab.

 newLoc = _locationManager.GetLastKnownLocation(_locationProvider); 
     if (newLoc != null) 
     { 
      _latitudeUser = newLoc.Latitude; 
      _longitudeUser = newLoc.Longitude; 
      _locationText.Text = string.Format("{0:f6},{1:f6}", newLoc.Latitude, newLoc.Longitude); 
     } 
     else 
     { 
      _locationProvider = LocationManager.NetworkProvider; 
      newLoc = _locationManager.GetLastKnownLocation(_locationProvider); 
      if (newLoc != null) 
      { 
       _latitudeUser = newLoc.Latitude; 
       _longitudeUser = newLoc.Longitude; 
       _locationText.Text = string.Format("{0:f6},{1:f6}", newLoc.Latitude, newLoc.Longitude); 
      } 
      else 
      { 
       _locationText.Text = string.Format("We currently cannot determine your location, please try again later."); 
      } 
+0

Es sieht so aus, als ob Sie eine andere Methode als ich verwenden, um überhaupt eine Position zu bekommen. Ich verwende den LocationManager nicht, da ich dachte, dass dies mit der Google Location API nicht kompatibel ist. –

+0

location = LocationServices.FusedLocationApi.getLastLocation (mGAC); ist was ich benutze –

Verwandte Themen