2017-02-06 8 views
-1

Ich baue eine App, die nach einem bestimmten Update Informationen auf dem Bildschirm anzeigt. Ich habe den Code sowohl für xML als auch für Java hinzugefügt und würde es begrüßen, wenn jemand mir sagen könnte, dass die Text-Ansichten und der Java-Code in Fettschrift korrekt sind.XML-Daten werden nicht aktualisiert, wenn der Standort geändert wird

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:padding="0dp"> 
    <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:src="@drawable/background" android:scaleType="centerCrop" /> 
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hiker's Watch" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30sp" android:textColor="#f3202020" android:layout_marginTop="30dp" /> 

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Latitude: " android:id="@+id/lat" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:textSize="20sp" android:textColor="#151515" /> 

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Longitude:" android:id="@+id/lng" android:layout_below="@+id/lat" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:textSize="20sp" android:textColor="#151515" /> 
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Accuracy: 20.0m" android:id="@+id/accuracy" android:layout_below="@+id/lng" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:textSize="20sp" android:textColor="#151515" /> 

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Speed:" android:id="@+id/speed" android:layout_below="@+id/accuracy" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:textSize="20sp" android:textColor="#151515" /> 

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bearing: " android:id="@+id/bearing" android:layout_below="@+id/speed" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:textSize="20sp" android:textColor="#151515" /> 

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Altitude:" android:id="@+id/altitude" android:layout_below="@+id/bearing" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:textSize="20sp" android:textColor="#151515" /> 

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Address: \n" android:id="@+id/address" android:layout_below="@+id/altitude" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:textSize="20sp" android:textColor="#151515" android:gravity="center_horizontal" /> 

</RelativeLayout> 

Java 

import android.app.Activity; 
import android.content.Context; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

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

public class MainActivity extends Activity implements LocationListener { 

    LocationManager locationManager; 
    String provider; 

    TextView latTV; 
    TextView lngTV; 
    TextView accuracyTV; 
    TextView speedTV; 
    TextView bearingTV; 
    TextView altitudeTV; 
    TextView addressTV; 

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

     latTV = (TextView) findViewById(R.id.lat); 
     lngTV = (TextView) findViewById(R.id.lng); 
     accuracyTV = (TextView) findViewById(R.id.accuracy); 
     speedTV = (TextView) findViewById(R.id.speed); 
     bearingTV = (TextView) findViewById(R.id.bearing); 
     altitudeTV = (TextView) findViewById(R.id.altitude); 
     addressTV = (TextView) findViewById(R.id.address); 


     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     provider = locationManager.getBestProvider(new Criteria(), false); 

     Location location = locationManager.getLastKnownLocation(provider); 

     onLocationChanged(location); 



    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     locationManager.removeUpdates(this); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     locationManager.requestLocationUpdates(provider, 400, 1, this); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

     Double lat = location.getLatitude(); 
     Double lng = location.getLongitude(); 
     Double alt = location.getAltitude(); 
     Float bearing = location.getBearing(); 
     Float speed = location.getSpeed(); 
     Float accuracy = location.getAccuracy(); 

     Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 

     try { 
      List<Address> listAddresses = geocoder.getFromLocation(lat, lng, 1); 

      if (listAddresses != null && listAddresses.size() > 0) { 

       Log.i("PlaceInfo", listAddresses.get(0).toString()); 

       String addressHolder = ""; 

       for (int i = 0; i <= listAddresses.get(0).getMaxAddressLineIndex(); i++) { 

        addressHolder += listAddresses.get(0).getAddressLine(i) + "\n"; 

       } 

       addressTV.setText("Address:\n" + addressHolder); 

      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     latTV.setText("Latitude: " + lat.toString()); 
     lngTV.setText("Longitude: " + lng.toString()); 
     altitudeTV.setText("Altitude: " + alt.toString() + "m"); 
     bearingTV.setText("Bearing: " + bearing.toString()); 
     speedTV.setText("Speed: " + speed.toString() + "m/s"); 
     accuracyTV.setText("Accuracy: " + accuracy.toString() + "m"); 



    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 
+0

Überlegen Sie, eine Antwort zu akzeptieren, wenn geholfen .. !! \ – W4R10CK

Antwort

0

Ihre String Parsen von Double und Float nicht innerhalb .setText(); korrigieren.

Korrigiert:

latTV.setText("Latitude: " + String.valueOf(lat)); 
lngTV.setText("Longitude: " + String.valueOf(lng)); 
altitudeTV.setText("Altitude: " + String.valueOf(alt) + "m"); 
bearingTV.setText("Bearing: " + Float.toString(bearing)); 
speedTV.setText("Speed: " + Float.toString(speed) + "m/s"); 
accuracyTV.setText("Accuracy: " + Float.toString(accuracy) + "m"); 
0

Sie Fused location api

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, LocationListener { 


    TextView txtOutputLat, txtOutputLon; 
    Location mLastLocation; 
    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 
    String lat, lon; 


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


     txtOutputLat = (TextView) findViewById(R.id.textView); 
     txtOutputLon = (TextView) findViewById(R.id.textView2); 


     buildGoogleApiClient(); 
    } 


    @Override 
    public void onConnected(Bundle bundle) { 


     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(100); // Update location every second 

     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 


     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      lat = String.valueOf(mLastLocation.getLatitude()); 
      lon = String.valueOf(mLastLocation.getLongitude()); 

     } 
     updateUI(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     lat = String.valueOf(location.getLatitude()); 
     lon = String.valueOf(location.getLongitude()); 
     //get other relevant data from location like bearing,address 
     updateUI(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     buildGoogleApiClient(); 
    } 

    synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 


    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     mGoogleApiClient.disconnect(); 
    } 

    void updateUI() { 
     txtOutputLat.setText(lat); 
     txtOutputLon.setText(lon); 
    } 
} 

Hier ein Detail Beispiel verwendet werden soll: http://www.androidwarriors.com/2015/10/fused-location-provider-in-android.html

Das Beste an Fused location provider API ist, dass man sich keine Sorgen brauchen, um über location Updates es gibt Ihnen immer aktuelle und genaue location und Updates location nach einem Intervall oder auf location ändern.

Eine weitere Sache über Fused location provider API ist, dass Sie nicht über die besten location Anbieter denken müssen, weil es automatisch am besten für Ihre Hardware Ihres Android-Geräts geeignet wählen.

Verwandte Themen