使用地图获取距离api android application

时间:2017-04-16 13:08:19

标签: android google-maps

我正在尝试使用纬度和经度来计算行驶距离。现在我知道我们可以使用map API的距离矩阵来完成它。但我的问题是,我不知道在哪里准确添加该链接。

这是我的MainActivity.java文件

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();
    }
}

我想它应该在getData()之后添加,因为我必须从服务器获取经度和经度进行计算。我是android的新手,这是我的第一个高级应用程序,并且已经尝试了很多代码。有人可以告诉我在上面的代码中应该如何以及在哪里添加它?

提前感谢您的帮助!!!

1 个答案:

答案 0 :(得分:0)

一旦你有了目的地lat lng,你就可以进行距离矩阵api调用

x86_64-w64-mingw32-