路线折线未实时显示

Route polyline is not displaying in real time

我的 google 地图仅在应用程序从后台进程恢复时显示创建的多段线(例如:屏幕关闭然后打开,或者弹出窗口 window 被关闭)

如何在用户移动时实时显示折线?这里似乎有什么问题?这是我的代码:有帮助吗?非常感谢!

public class MapsActivity extends FragmentActivity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {


public Polyline lineRoute;
int flag;
LatLng prev = new LatLng(0, 0);
List<LatLng> points = new ArrayList<>();
public static final String TAG = MapsActivity.class.getSimpleName();
PolylineOptions rectOptions = new PolylineOptions().width(3).color(Color.RED);

private boolean drawTrack = true;
private PolylineOptions routeOpts = null;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

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

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

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds
}

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

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

    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}
 private void setUpMap() {
    mMap.setMyLocationEnabled(true);
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    lineRoute = mMap.addPolyline(rectOptions);
}

private void handleNewLocation(Location location) {
    points = lineRoute.getPoints();
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();
    LatLng latLng = new LatLng(currentLatitude, currentLongitude);
    points.add(latLng);
    lineRoute.setPoints(points);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 25));
}

@Override
public void onConnected(Bundle bundle) {
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location == null) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
    else {
        handleNewLocation(location);
    }
}

@Override
public void onConnectionSuspended(int i) {

}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    /*
     * Google Play services can resolve some errors it detects.
     * If the error has a resolution, try sending an Intent to
     * start a Google Play services activity that can resolve
     * error.
     */
    if (connectionResult.hasResolution()) {
        try {
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
            /*
             * Thrown if Google Play services canceled the original
             * PendingIntent
             */
        } catch (IntentSender.SendIntentException e) {
            // Log the error
            e.printStackTrace();
        }
    } else {
        /*
         * If no resolution is available, display a dialog to the
         * user with the error.
         */
        Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
    }
}

@Override
public void onLocationChanged(Location location) {
    handleNewLocation(location);
}
}

您看得不正确,因为在任何 "handleNewLocation" 上您: 创建一个新的点列表(显然是空的) 然后你得到当前位置,放入这个空列表。清除之前的多段线并显示一条多段线,其中填充了之前的点(这是一个只有一个点的列表);这就是为什么是空的。 为了正确查看它,您可以将点保留在函数范围之外或避免清除地图,而是使用 "lineRoute" 参数添加点。

private void handleNewLocation(Location location) {
    ArrayList<LatLng> points = lineRoute.getPoints(); // getPoints() gives you a COPY of the points
    //points = new ArrayList<LatLng>();
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();
    LatLng latLng = new LatLng(currentLatitude, currentLongitude);
    //mMap.clear();
    points.add(latLng);
    //rectOptions.addAll(points);
    //mMap.addPolyline(rectOptions);
    lineRoute.setPoints(points);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 25));
}

这应该能帮到你。 显然我没有涉及清除折线等,这取决于您的应用程序逻辑

Try on this way.

package com.ibl.googlemap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.ibl.comman.GPSTracker;
import com.ibl.foodpulse.CreateShareDeal;
import com.ibl.foodpulse.R;

public class ViewMapDirection extends FragmentActivity implements
    OnClickListener {

private GoogleMap map;
private ArrayList<LatLng> markerPoints;
private Button btnShow;
private double ans;

private GPSTracker gps;
private double latitude2, latitude, longitude;
private double longitude2;
private int bcid;
private int vuid;
String res = "";
String url2;

Boolean checknet = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.googlemap_only_activity);
    gps = new GPSTracker(ViewMapDirection.this);

    Bundle b = getIntent().getExtras();
    latitude = b.getFloat("lat"); // hear i got lat lng from last activity.
    longitude = b.getFloat("longy");
    Log.e("View Map Direction", "get the Lat and Lng :" + latitude + " :: "
            + longitude);

    if (gps.canGetLocation()) {
        latitude2 = gps.getLatitude();
        longitude2 = gps.getLongitude();
        // hear i will got current location from GPS Tracker
        Log.e("View Map Direction ", " get Current Lat lng " + latitude2
                + " lng " + longitude2);
    } else {
        gps.showSettingsAlert(ViewMapDirection.this);
    }

    Intent j = getIntent();
    bcid = j.getIntExtra("BCARD_ID", 0);
    vuid = j.getIntExtra("VUID1", 0);
    btnShow = (Button) findViewById(R.id.btn_onlywebview_close);
    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map_googlemapAct);

    // Getting Map for the SupportMapFragment
    map = fm.getMap();
    /*
     * db=new MyDBHandler(this); gp=db.getGPSdata(bcid);
     */
    btnShow.setOnClickListener(this);

}

// Initializing
private String getDirectionsUrl(LatLng origin, LatLng dest) {

    // Origin of route
    String str_origin = "origin=" + origin.latitude + ","
            + origin.longitude;

    // Destination of route
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

    // Sensor enabled
    String sensor = "sensor=false";

    // Building the parameters to the web service
    String parameters = str_origin + "&" + str_dest + "&" + sensor;

    // Output format
    String output = "json";

    // Start

    int Radius = 6371;// radius of earth in Km
    double lat1 = origin.latitude;
    double lat2 = dest.latitude;
    double lon1 = origin.longitude;
    double lon2 = dest.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    int meterInDec = Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
            + " Meter   " + meterInDec);
    ans = Radius * c;
    // Toast.makeText(getApplicationContext(), "Result" + ans,
    // Toast.LENGTH_SHORT).show();

    // End

    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/directions/"
            + output + "?" + parameters;

    return url;
}

/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try {
        URL url = new URL(strUrl);

        // Creating an http connection to communicate with url
        urlConnection = (HttpURLConnection) url.openConnection();

        // Connecting to url
        urlConnection.connect();

        // Reading data from url
        iStream = urlConnection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                iStream));

        StringBuffer sb = new StringBuffer();

        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        data = sb.toString();

        br.close();

    } catch (Exception e) {
        // Log.d("Exception while downloading url", e.toString());
    } finally {
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    markerPoints = new ArrayList<LatLng>();

    // Getting reference to SupportMapFragment of the
    // activity_main

    if (map != null) {
        Log.e(" in View Map ", " in On Resume Method calling .........");
        // if (gps.canGetLocation()) {
        // latitude2 = gps.getLatitude();
        // longitude2 = gps.getLongitude();

        // Enable MyLocation Button in the Map
        map.setMyLocationEnabled(true);
        // Toast.makeText(getApplicationContext(),"Card Lattitude is "+gp.getGps_lattitude()+" LOngitude is "+gp.getGps_longitude(),Toast.LENGTH_LONG).show();
        LatLng CardPoint = new LatLng(latitude, longitude);
        // LatLng CardPoint=new
        // LatLng(21.237425849325856,72.8330023214221);

        Log.e(" in View Map ", " in On Card Point........."
                + CardPoint.latitude);

        MarkerOptions Destpoint = new MarkerOptions();
        Destpoint.position(CardPoint);
        map.addMarker(Destpoint);
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(CardPoint, 10));
        // Toast.makeText(getApplicationContext(),"Visitor Lattitude is "+latitude2+" LOngitude is "+longitude2,Toast.LENGTH_LONG).show();
        LatLng UserPoint = new LatLng(latitude2, longitude2);
        MarkerOptions Sourcepoint = new MarkerOptions();
        Sourcepoint.position(UserPoint);
        map.addMarker(Sourcepoint);

        String url = getDirectionsUrl(UserPoint, CardPoint);
        DownloadTask downloadTask = new DownloadTask();
        downloadTask.execute(url);
        // } else {
        // gps.showSettingsAlert(ViewMapDirection.this);
        // }
    }
}

// Fetches data from url passed
private class DownloadTask extends AsyncTask<String, Void, String> {

    // Downloading data in non-ui thread
    @Override
    protected String doInBackground(String... url) {

        // For storing data from web service
        String data = "";
        Log.e(" in View Map ",
                " in Download Task  ........." + url.toString());
        try {
            // Fetching the data from web service
            data = downloadUrl(url[0]);
        } catch (Exception e) {
            Log.e("Background Task", e.toString());
        }
        return data;
    }

    // Executes in UI thread, after the execution of
    // doInBackground()
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        ParserTask parserTask = new ParserTask();
        Log.e(" in View Map ", " in on Post  .........");
        // Invokes the thread for parsing the JSON data
        parserTask.execute(result);
    }
}

/** A class to parse the Google Places in JSON format */
private class ParserTask extends
        AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

    // Parsing the data in non-ui thread
    @Override
    protected List<List<HashMap<String, String>>> doInBackground(
            String... jsonData) {

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;
        Log.e(" in View Map ", " in parser Task Calling  .........");
        try {
            jObject = new JSONObject(jsonData[0]);
            DirectionsJSONParser parser = new DirectionsJSONParser();
            // Starts parsing data
            routes = parser.parse(jObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return routes;
    }

    // Executes in UI thread, after the parsing process
    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result)     {
        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;
        MarkerOptions markerOptions = new MarkerOptions();
        Log.e(" in View Map ", " in parser post ........." + result.size());
        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) {
            Log.e(" in View Map ", " in Result For .........");
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            Log.e(" in View Map ", " in path  ........." + path.size());
            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                Log.e("in View Direction ", " lag ,,,,, " + lat
                        + " lng ... " + lng);
                points.add(position);
            }

            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            lineOptions.width(4);
            lineOptions.color(Color.BLUE);
        }

        // Drawing polyline in the Google Map for the i-th route
        // if (gps.canGetLocation()) {
        // latitude2 = gps.getLatitude();
        // longitude2 = gps.getLongitude();
        if (map != null) {
            if (gps.canGetLocation()) {
                latitude2 = gps.getLatitude();
                longitude2 = gps.getLongitude();

                Log.e("View Map Direction ", " get Current Lat lng "
                        + latitude2 + " lng " + longitude2);
                if ((latitude != 0) && (longitude != 0)) {
                    map.addPolyline(lineOptions);
                }
            } else {
                gps.showSettingsAlert(ViewMapDirection.this);
            }
        }
        // } else {
        // gps.showSettingsAlert(ViewMapDirection.this);
        // }
        // onResume();
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_onlywebview_close:
        finish();
        break;
    default:
        break;
    }

}

}

这是另一个 class 用于过去的命令 ...

package com.ibl.googlemap;

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

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

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

public class DirectionsJSONParser {

/**
 * Receives a JSONObject and returns a list of lists containing latitude and
 * longitude
 */
public List<List<HashMap<String, String>>> parse(JSONObject jObject) {

    List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>();
    JSONArray jRoutes = null;
    JSONArray jLegs = null;
    JSONArray jSteps = null;

    try {

        jRoutes = jObject.getJSONArray("routes");

        /** Traversing all routes */
        for (int i = 0; i < jRoutes.length(); i++) {
            jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
            List path = new ArrayList<HashMap<String, String>>();

            /** Traversing all legs */
            for (int j = 0; j < jLegs.length(); j++) {
                jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps");

                /** Traversing all steps */
                for (int k = 0; k < jSteps.length(); k++) {
                    String polyline = "";
                    polyline = (String) ((JSONObject) ((JSONObject) jSteps
                            .get(k)).get("polyline")).get("points");
                    List<LatLng> list = decodePoly(polyline);

                    /** Traversing all points */
                    for (int l = 0; l < list.size(); l++) {
                        HashMap<String, String> hm = new HashMap<String, String>();
                        hm.put("lat",
                                Double.toString(((LatLng) list.get(l)).latitude));
                        hm.put("lng",
                                Double.toString(((LatLng) list.get(l)).longitude));
                        path.add(hm);
                    }
                }
                routes.add(path);
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
    }

    return routes;
}

/**
 * Method to decode polyline points Courtesy :
 * http://jeffreysambells.com/2010
 * /05/27/decoding-polylines-from-google-maps-direction-api-with-java
 * */
private List<LatLng> decodePoly(String encoded) {

    List<LatLng> poly = new ArrayList<LatLng>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;

    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;

        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;

        LatLng p = new LatLng((((double) lat / 1E5)),
                (((double) lng / 1E5)));
        poly.add(p);
    }

    return poly;
}

}