Android Google地图折线未在地图上显示

时间:2015-09-09 15:42:36

标签: java android google-maps google-roads-api

我创建了一个具有方法和locaton侦听器的类。 locationListener将每2秒执行一次,获取lat long并将其传递给异步任务。 Async Task类会将lat long传递给Google Road Api以获取快照lat long并将lat long传递给postExecute()

PostExecute()应该绘制折线,但它不会..

请参阅以下代码:

MapsActivity.java

package com.example.akshay.roadsapi;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    final String URL = "https://roads.googleapis.com/v1/snapToRoads?path=";
    final String KEY = "API KEY HERE";
    int i = 0;
    AsyncTask<Void, Void, LatLng> latLng1 = null;
    Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

                BackgroundTask backgroundTask = new BackgroundTask(MapsActivity.this, latLng, URL, KEY, activity, mMap);

                    backgroundTask.execute();


                /*mMap.addMarker(new MarkerOptions().position(latLng1));*/
            }


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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        });

    }

}

BackgroundTask.java

package com.example.akshay.roadsapi;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by Akshay on 9/8/2015.
 */
public class BackgroundTask extends AsyncTask<Void, Void, LatLng> {
    Context CONTEXT;
    LatLng LATLNGS;
    String URL;
    String KEY;
    Double LAT = null;
    Double LONG = null;
    GoogleMap map;
    Activity ACTIVITY;
    myInt myInt = null;
    int flag = 0;
    LatLng prev;
    LatLng latlng;

    public BackgroundTask(Context context, LatLng LATLNGS, String URL, String KEY, Activity ACTIVITY, GoogleMap map) {
        this.CONTEXT = context;
        this.LATLNGS = LATLNGS;
        this.URL = URL;
        this.KEY = KEY;
        this.ACTIVITY = ACTIVITY;
        this.map = map;

    }


    @Override
    protected LatLng doInBackground(Void... params) {


        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet();
        HttpResponse response = null;
        HttpEntity entity = null;
        InputStream inputStreamReader = null;
        BufferedReader bufferedReader = null;
        String line = null;
        StringBuilder strin = new StringBuilder();

        try {

            get.setURI(new URI(URL + LATLNGS.latitude + "," + LATLNGS.longitude + "&interpolate=false&key=" + KEY));
            response = client.execute(get);
            entity = response.getEntity();
            inputStreamReader = entity.getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStreamReader));
            while ((line = bufferedReader.readLine()) != null) {
                strin.append(line + "\n");
            }

            Log.e("============", strin.toString());

            JSONObject ob = new JSONObject(strin.toString());
            JSONArray array = ob.getJSONArray("snappedPoints");
            for (int i = 0; i <= array.length(); i++) {
                JSONObject object = array.getJSONObject(i).getJSONObject("location");
                LAT = object.getDouble("latitude");
                LONG = object.getDouble("longitude");

            }

        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        latlng= new LatLng(LAT, LONG);
        return latlng;

    }

    @Override
    protected void onPostExecute(LatLng latLng) {
        super.onPostExecute(latLng);
       /* MarkerOptions markerOptions = new MarkerOptions().position(latLng);
        map.addMarker(markerOptions);*/




        if (flag == 0) {
            prev = latLng;
            flag = 1;
        }
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
       /* Marker marker = map.addMarker(new MarkerOptions().position(latLng));*/


        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.add(prev, latLng).color(Color.BLUE).width(50).visible(true);
        map.addPolyline(polylineOptions);
        map.animateCamera(cameraUpdate);




        prev = latLng;


        latLng = null;

    }
}

我从GOOGLE API获取Json数组并成功获得lats long值但不绘制折线.. 请帮忙

由于

0 个答案:

没有答案