在 Android 上更改位置时不断更新标记

Posted

技术标签:

【中文标题】在 Android 上更改位置时不断更新标记【英文标题】:Continuously update marker when location changed on Android 【发布时间】:2014-09-19 13:17:15 【问题描述】:

我正在尝试使用 Google 地图实现一个简单的导航应用。最初,我开发了一个代码,用于在地图上标记两个地理点并显示它们之间的路线。 现在我正在尝试根据用户位置将第一个标记移向第二个标记(目的地)。

为此,我使用了LocationListener 并在onLocationChanged 中实现了代码。 问题是onLocationChanged 没有被解雇,并且标记没有根据用户位置移动。 这是我的代码。

MapActivity.java

public class MapActivity extends FragmentActivity implements OnMapClickListener, LocationListener

private GoogleMap gMap;
private ArrayList<LatLng> markersArray; //Marker points ArrayList
private ArrayList<Marker> markers;
private MarkerOptions options; //Marker options
String distance="";
String duration="";
private TextView travelDetails;
private ProgressDialog pDialog;
private double lattitude=0.0;
private double longitude=0.0;
Marker mMarker;
String bestAvailableProvider;
private LocationManager mLocationManager;


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

    //Loading Google Maps
    try
        loadMapView();
        markersArray=new ArrayList<LatLng>();
        markers=new ArrayList<Marker>();
        gMap.setOnMapClickListener(this);
        gMap.setMyLocationEnabled(true);
        gMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        travelDetails=(TextView) findViewById(R.id.lbl_traveldetails);
        pDialog=new ProgressDialog(MapActivity.this);
    catch(Exception e)
        e.printStackTrace();
    



/*
 * This method loads map
 * 
 */

private void loadMapView()

    if (gMap==null) 
        gMap=((SupportMapFragment)getSupportFragmentManager()
                .findFragmentById(R.id.map_fragment)).getMap();
    


    mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    // Specify Location Provider criteria
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setSpeedRequired(false);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this);





/*
 * catches onclick event of map
 * @see com.google.android.gms.maps.GoogleMap.OnMapClickListener#onMapClick(com.google.android.gms.maps.model.LatLng)
 */

@Override
public void onMapClick(LatLng point) 

    //Clearing markers in map


    if (markersArray.size()>1) 
        markersArray.clear();
        gMap.clear();

    

    //Adding  marker to array
    markersArray.add(point);


    //marking point on Map
    addMarkerOnMap(point);



    //Drawing route when two markers are placed

    if(markersArray.size() >= 2)
        LatLng origin = markersArray.get(0);
        LatLng dest = markersArray.get(1);

        String url = getDirectionsUrl(origin, dest);

        DownloadTask downloadTask = new DownloadTask();

        downloadTask.execute(url);
    





/*
 * This method places markers on map
 */


private void addMarkerOnMap(LatLng point)

    //Marking points on map
    options=new MarkerOptions();
    options.position(point);

    //Setting marker icons

    if (markersArray.size()==1) 

        //Source marker point
        options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));


    else if(markersArray.size()==2)

        //Destination marker point
        options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
    

    //adding marker point to map
    try
        Marker mMarker=gMap.addMarker(options);

        markers.add(mMarker);
    catch(Exception e)
        e.printStackTrace();
    




/*
 * Getting direction url between two points
 * 
 */

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";

    String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;

    return url;



/*
 * passing the direction url 
 * 
 */


private class DownloadTask extends AsyncTask<String, Void, String>


    @Override
    protected void onPreExecute() 
        super.onPreExecute();
        pDialog.show();
        pDialog.setMessage("Loading route...");
        pDialog.setCancelable(false);
    

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

        // For storing data from web service
        String data = "";

        try
            // Fetching the data from web service
            data = downloadUrl(url[0]);
        catch(Exception e)
            Log.d("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();

        // Invokes the thread for parsing the JSON data
        parserTask.execute(result);
    


/*
 * downloading from url 
 * 
 */


private String downloadUrl(String strUrl) throws IOException
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try
        URL url = new URL(strUrl);

        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;



private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>>>

    @Override
    protected void onPreExecute() 
        super.onPreExecute();
    

    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) 

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;

        try
            jObject = new JSONObject(jsonData[0]);
            DirectionsJsonParser parser = new DirectionsJsonParser();

            // Starts parsing data
            routes = parser.parse(jObject);
        catch(Exception e)
            e.printStackTrace();
        
        return routes;
    

    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) 
        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;
        //  MarkerOptions markerOptions = new MarkerOptions();

        // Traversing through all the routes
        for(int i=0;i<result.size();i++)
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

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

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


                if(j==0)    // Get distance from the list
                    distance = (String)point.get("distance");
                    continue;
                else if(j==1) // Get duration from the list
                    duration = (String)point.get("duration");
                    continue;
                

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

                points.add(position);
            

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

        // Drawing polyline in the Google Map for the i-th route
        try
            gMap.addPolyline(lineOptions);
            travelDetails.setText("Distance:"+distance+"Duration:"+duration);
            pDialog.dismiss();
        catch (Exception e) 
            if (pDialog.isShowing()) 
                pDialog.dismiss();
            
            Toast.makeText(getApplicationContext(), "No Road route found.", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        
    






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

    loadMapView();


@Override
protected void onPause() 
    super.onPause();
    mLocationManager.removeUpdates(this);


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


/*
 * (non-Javadoc)
 * @see com.google.android.gms.maps.LocationSource.OnLocationChangedListener#onLocationChanged(android.location.Location)
 */

@Override
public void onLocationChanged(Location location) 
    Log.e("loca", ""+location.getLatitude());

    try 


        if(markersArray.size()>0)

            lattitude=location.getLatitude();
            longitude=location.getLongitude();

            LatLng mLatLng=new LatLng(lattitude, longitude);
            Marker newMarker=markers.get(0);
            newMarker.setPosition(mLatLng);
            gMap.animateCamera(CameraUpdateFactory.newLatLng(mLatLng));
        

     catch (Exception e) 
        e.printStackTrace();
    



@Override
public void onProviderDisabled(String provider) 
    // TODO Auto-generated method stub



@Override
public void onProviderEnabled(String provider) 
    // TODO Auto-generated method stub


@Override
public void onStatusChanged(String provider, int status, Bundle extras) 
    // TODO Auto-generated method stub





DirectionsJsonParser.java

public class DirectionsJsonParser 
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;
    JSONObject jDistance = null;
    JSONObject jDuration = null;

    try 

        jRoutes = jObject.getJSONArray("routes");


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


            for(int j=0;j<jLegs.length();j++)
                jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");

                /** Getting distance from the json data */
                jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance");
                HashMap<String, String> hmDistance = new HashMap<String, String>();
                hmDistance.put("distance", jDistance.getString("text"));

                /** Getting duration from the json data */
                jDuration = ((JSONObject) jLegs.get(j)).getJSONObject("duration");
                HashMap<String, String> hmDuration = new HashMap<String, String>();
                hmDuration.put("duration", jDuration.getString("text"));

                /** Adding distance object to the path */
                path.add(hmDistance);

                /** Adding duration object to the path */
                path.add(hmDuration);

                /** Getting poly points */
                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);


                    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);
                    
                
                /** Adding poly points */
                routes.add(path);
            
        

     catch (JSONException e) 
        e.printStackTrace();
    catch (Exception e)
    
    return routes;


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;



【问题讨论】:

【参考方案1】:

我认为你没有使用位置提供程序,使用 googleapiclient

并阅读有关此主题的信息,它允许您在位置更改时在地图上显示您的当前位置,从而为您提供当前位置。

提示

实现 GoogleApiClient.ConnectionCallbacks , GoogleApiClient.OnConnectionFailedListener 在您的活动中,您可以轻松地做到这一点。

【讨论】:

以上是关于在 Android 上更改位置时不断更新标记的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 中步行/驾驶时更新位置标记并在 Google 地图上绘制路径

如何在不更改用户设置的缩放比例的情况下更新地图标记?

如何在 Android 的 MapView 中更新当前位置的蓝点标记

InfoWindow中的Android Google地图摘要自动更新

如何更改标记在 Android Map v2 上的位置

如何根据“位置更改”不断更新表格视图?