2017-04-16 4 views
0

Ich versuche, die Fahrstrecke mit Breiten- und Längengrad zu berechnen. Jetzt weiß ich, dass wir die Distanzmatrix der Karten-API dazu verwenden können. Aber mein Problem ist, ich weiß nicht, wo genau ich diesen Link hinzufügen soll.Entfernung mit Karten api Android-Anwendung

Das ist mein MainActivity.java Datei

package com.example.john.bustrackclient; 

import android.Manifest; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.StringRequest; 
import com.android.volley.toolbox.Volley; 

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

import static com.android.volley.Request.Method.GET; 
import static com.example.john.bustrackclient.R.id.textView4; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener,LocationListener { 

    LocationManager locationManager; 
    String mprovider; 
    String latitude,longitude,latitude1,longitude1; 

    private EditText editTextId; 
    private Button buttonGet; 
    private TextView textViewResult; 

    private ProgressDialog loading; 

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

     editTextId = (EditText) findViewById(R.id.editTextId); 
     buttonGet = (Button) findViewById(R.id.buttonGet); 
     textViewResult = (TextView) findViewById(R.id.textViewResult); 

     buttonGet.setOnClickListener(this); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 

     mprovider = locationManager.getBestProvider(criteria, false); 

     if (mprovider != null && !mprovider.equals("")) { 
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       return; 
      } 
      Location location = locationManager.getLastKnownLocation(mprovider); 
      locationManager.requestLocationUpdates(mprovider, 15000, 1, this); 

      if (location != null) 
       onLocationChanged(location); 
      else 
       Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    public void onLocationChanged(Location location) { 
     TextView latitude = (TextView) findViewById(R.id.textView2); 
     TextView longitude = (TextView) findViewById(R.id.textView3); 

     latitude.setText("Latitude: " + location.getLatitude()); 
     longitude.setText("Longitude: " + location.getLongitude()); 

    } 

    public void onStatusChanged(String s,int i,Bundle bundle) 
    {} 

    public void onProviderEnabled(String s) 
    {} 

    public void onProviderDisabled(String s) 
    {} 


    private void getData() { 
     String destination = editTextId.getText().toString().trim(); 
     if (destination.equals("")) { 
      Toast.makeText(this, "Please enter your destination", Toast.LENGTH_LONG).show(); 
      return; 
     } 
     loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false); 

     String url = Config.DATA_URL+editTextId.getText().toString().trim(); 

     StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       loading.dismiss(); 
       showJSON(response); 
      } 
     }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); 
        } 
       }); 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 
    } 

    private void showJSON(String response){ 
     String latitude1=""; 
     String longitude1=""; 
     try { 
      JSONObject jsonObject = new JSONObject(response); 
      JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY); 
      JSONObject updata = result.getJSONObject(0); 
      latitude1 = updata.getString(Config.KEY_LATITUDE); 
      longitude1 = updata.getString(Config.KEY_LONGITUDE); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     textViewResult.setText("Latitude: "+latitude1+"\nLongitude: "+longitude1); 
    } 

    @Override 
    public void onClick(View v) 
    { 
     getData(); 
    } 
} 

Ich denke, es sollte nach getData() hinzugefügt werden, da ich die geografische Breite und Länge vom Server für die Berechnung bekommen haben. Ich bin sehr neu in Android und das ist meine erste Art von fortgeschrittenen Anwendung und versuchte schon einige Codes. Kann mir bitte jemand zeigen, wie und wo es im obigen Code hinzugefügt werden sollte?

Vielen Dank im Voraus für jede Hilfe !!!

Antwort

0

, sobald Sie das Ziel haben lat lng können Sie api Distanzmatrix Anruf

 String origin = null; 
    String destination = null; 

    if (mCurrentLocation != null && destLtLng != null) { 
     origin = mCurrentLocation.getLatitude() + Constants.COMMA + mCurrentLocation.getLongitude(); 
     destination = destLtLng.latitude + Constants.COMMA 
       + destLtLng.longitude; 
+0

Vielen Dank für Ihre Hilfe machen !!! Aber sollte es nicht ein Link sein? (Https://maps.googleapis.com/maps/api/distancatrix/json?units=imperial&origins=latitude,longitude&destinations=latitude,longitude&key=YOUR_API_KEY) Soll ich einfach hinzufügen der obige Code? – John

Verwandte Themen