2013-04-28 90 views
11

我想进行方向应用,但是,我从我的位置到目的地绘制路线时遇到问题,我从我的位置获取了自变量经度和纬度,但我不知道画线..我想绘制方向到这个位置= -6.984873352070259,108.48140716552734。请帮我家伙..我已经阅读之前quetions但..感谢.. IAM抱歉之前我不能得到解决。这里是我的代码在android系统编程,我希望你能帮助我如何从我的位置在Google Maps API V2中绘制路线

package com.apps.visitkuningan; 

import android.app.Dialog; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
import android.widget.Toast; 


import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 

public class Arahkan extends FragmentActivity implements LocationListener { 

    GoogleMap googleMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // Getting Google Play availability status 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 

     // Showing status 
     if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available 

      int requestCode = 10; 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
      dialog.show(); 

     }else { // Google Play Services are available 

      // Getting reference to the SupportMapFragment of activity_main.xml 
      SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 

      // Getting GoogleMap object from the fragment 
      googleMap = fm.getMap(); 

      // Enabling MyLocation Layer of Google Map 
      googleMap.setMyLocationEnabled(true); 

      // Getting LocationManager object from System Service LOCATION_SERVICE 
      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

      // Creating a criteria object to retrieve provider 
      Criteria criteria = new Criteria(); 

      // Getting the name of the best provider 
      String provider = locationManager.getBestProvider(criteria, true); 

      // Getting Current Location 
      Location location = locationManager.getLastKnownLocation(provider); 

      if(location!=null){ 
       onLocationChanged(location); 
      } 
      locationManager.requestLocationUpdates(provider, 20000, 0, this); 
     } 
    } 
    @Override 
    public void onLocationChanged(Location location) { 

     // Getting latitude of the current location 
     double latitude = location.getLatitude(); 

     // Getting longitude of the current location 
     double longitude = location.getLongitude(); 

     // Creating a LatLng object for the current location 
     LatLng latLng = new LatLng(latitude, longitude); 

     // Showing the current location in Google Map 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

     // Zoom in the Google Map 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 

    } 

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

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


} 

荫新手。 。谢谢.. :)

+0

参阅下面的链接, [绘制路径] [1] [1]:http://stackoverflow.com/questions/14702621/answer-draw-path-between-two -points-using-google-maps-android-api-v2 – 2014-03-24 09:49:05

回答

8

首先,您可以使用Google路线API获取两个位置坐标之间的方向。

public static ArrayList getDirections(double lat1, double lon1, double lat2, double lon2) { 
    String url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" +lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric"; 
    String tag[] = { "lat", "lng" }; 
    ArrayList list_of_geopoints = new ArrayList(); 
    HttpResponse response = null; 
    try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpPost httpPost = new HttpPost(url); 
     response = httpClient.execute(httpPost, localContext); 
     InputStream in = response.getEntity().getContent(); 
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document doc = builder.parse(in); 
     if (doc != null) { 
      NodeList nl1, nl2; 
      nl1 = doc.getElementsByTagName(tag[0]); 
      nl2 = doc.getElementsByTagName(tag[1]); 
      if (nl1.getLength() > 0) { 
       list_of_geopoints = new ArrayList(); 
       for (int i = 0; i < nl1.getLength(); i++) { 
        Node node1 = nl1.item(i); 
        Node node2 = nl2.item(i); 
        double lat = Double.parseDouble(node1.getTextContent()); 
        double lng = Double.parseDouble(node2.getTextContent()); 
        list_of_geopoints.add(new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6))); 
       } 
      } else { 
       // No points found 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return list_of_geopoints; 
} 

一旦在Android应用程序中创建了MapView布局,就可以包含此自定义Overlay类。

public class MyOverlay extends Overlay { 

private ArrayList all_geo_points; 

public MyOverlay(ArrayList allGeoPoints) { 
    super(); 
    this.all_geo_points = allGeoPoints; 
} 

@Override 
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) { 
    super.draw(canvas, mv, shadow); 
    drawPath(mv, canvas); 
    return true; 
} 

public void drawPath(MapView mv, Canvas canvas) { 
    int xPrev = -1, yPrev = -1, xNow = -1, yNow = -1; 
    Paint paint = new Paint(); 
    paint.setColor(Color.BLUE); 
    paint.setStyle(Paint.Style.FILL_AND_STROKE); 
    paint.setStrokeWidth(4); 
paint.setAlpha(100); 
    if (all_geo_points != null) 
     for (int i = 0; i < all_geo_points.size() - 4; i++) { 
      GeoPoint gp = all_geo_points.get(i); 
      Point point = new Point(); 
      mv.getProjection().toPixels(gp, point); 
      xNow = point.x; 
      yNow = point.y; 
      if (xPrev != -1) { 
       canvas.drawLine(xPrev, yPrev, xNow, yNow, paint); 
      } 
      xPrev = xNow; 
      yPrev = yNow; 
     } 
    } 
} 

在将此覆盖图添加到MapView覆盖图之前,可以调用getDirections()函数。

MapView mv = (MapView) findViewById(R.id.mvGoogle); 
mv.setBuiltInZoomControls(true); 
MapController mc = mv.getController(); 
ArrayList all_geo_points = getDirections(17.3849, 78.4866, 28.63491, 77.22461); 
GeoPoint moveTo = all_geo_points.get(0); 
mc.animateTo(moveTo); 
mc.setZoom(12); 
mv.getOverlays().add(new MyOverlay(all_geo_points)); 
+0

什么是HttpClient和HttpResponse? – 2015-08-19 13:30:11

+0

执行URL调用的对象。请参阅https://developers.google.com/maps/documentation/directions/ v – 2015-11-23 22:54:06

14

假设你至少有2个位置的对象,您可以绘制折线。 此方法在地图上绘制一个半透明的蓝色线条,给出位置列表 。此代码取自目前在Android Play商店(简单步行)的 应用程序。

private void drawPrimaryLinePath(ArrayList<Location> listLocsToDraw) 
{ 
    if (map == null) 
    { 
     return; 
    } 

    if (listLocsToDraw.size() < 2) 
    { 
     return; 
    } 

    PolylineOptions options = new PolylineOptions(); 

    options.color(Color.parseColor("#CC0000FF")); 
    options.width(5); 
    options.visible(true); 

    for (Location locRecorded : listLocsToDraw) 
    { 
     options.add(new LatLng(locRecorded.getLatitude(), 
           locRecorded.getLongitude())); 
    } 

    map.addPolyline(options); 

} 
+0

您是否需要解码路径? https://developers.google.com/maps/documentation/utilities/polylinealgorithm – 2015-08-19 13:16:38

相关问题