2013-03-18 60 views
1

如何使用google maps api v2绘制从当前位置到目的地的路线方向?我想从我的位置画到我的目的地。 这是我的第一个应用程序,请帮助我..如何绘制从当前位置到目的地的路线方向

这里我的代码:

package iqbal.map.googlemap; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
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 android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 

    private GoogleMap mMap; 
    static final LatLng HAMBURG = new LatLng(-6.964697,108.468684); 

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

     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 


     Marker hamburg = mMap.addMarker(new MarkerOptions() 
     .position(HAMBURG) 
     .title("Hamburg") 
     .snippet("Kiel is cool") 
     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 

      // Move the camera instantly to hamburg with a zoom of 15. 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 5)); 

     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(KIEL, 5)); 

     // Zoom in, animating the camera. 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null); 

    } 

    @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; 
    } 

} 

此代码仅适用于附加标记..如何从当前位置绘制方向目的地..谢谢..

回答

1

有这样做的多种方式..

  • 使用的浏览器,通源和目的地

    public void showDirections(View view) { 
        final Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?" + "saddr="+ latitude + "," + longitude + "&daddr=" + latitude + "," + longitude)); 
    intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity"); 
    startActivity(intent); 
    } 
    
  • 在地图中使用投影请参阅How to draw straight path between two points。它不是你的问题的完美解决方案,但你会明白。

+0

我如何在地图上获得我的位置 – 2013-03-18 04:43:43

+0

总是有两点需要1)您当前的位置2)目的地位置。你可以得到你的位置使用位置管理器 – Sameer 2013-03-18 04:46:41

+0

什么功能.. ..上面的代码.. Iam对不起Iam在编程Android新手.. – 2013-03-18 04:56:47

相关问题