2013-03-29 40 views
2

我想製作一個應用程序來顯示旅行時間和距離以及用戶位置到目的地位置的路徑。谷歌地圖api版本2中的距離和旅行時間android

這裏是我TampilkanMap類:

public class TampilkanMap extends FragmentActivity { 
    GoogleMap googleMap; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map); 

     SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 

     googleMap = fm.getMap(); 

     // Enabling MyLocation Layer of Google Map 
     googleMap.setMyLocationEnabled(true); 
     // lokasi wisata 
     Marker GunungButtuKabombong = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-3.568419, 119.777141)).title("Enrekang") 
       .snippet("Gunung Buttu kabobong")); 
     Marker HutanMalino = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-5.296776, 119.740562)).title("Gowa") 
       .snippet("Hutan Malino")); 
     Marker SombaOpu = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-5.191144, 119.407899)).title("Makassar") 
       .snippet("Benteng Somba Opu")); 
     Marker Londa = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-2.968156, 119.900479)).title("Rantepao") 
       .snippet("Londa")); 
     Marker Jompie = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-4.005563, 119.634331)).title("Parepare") 
       .snippet("Hutan Jompie")); 
     Marker Lumpue = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-4.057233, 119.622411)).title("Parepare") 
       .snippet("Pantai Lumpue")); 
     Marker Bira = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-5.550204, 120.206166)).title("Bulukumba") 
       .snippet("Pantai Tanjung Bira")); 
     Marker Losari = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-5.133219, 119.41662)).title("Makassar") 
       .snippet("Pantai Losari")); 
     Marker Bantimurung = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-5.006130, 119.650970)).title("Maros") 
       .snippet("Air Terjun Bantimurung")); 
     Marker Kelelawar = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-4.349835, 119.927759)).title("Soppeng") 
       .snippet("Kelelawar")); 
     Marker DanauTempe = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-4.110587, 119.953579)).title("Wajo") 
       .snippet("Danau Tempe")); 
     Marker FortRotterdam = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(-5.134148, 119.405276)).title("Makassar") 
       .snippet("Benteng Fort Rotterdam")); 
    } 

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

    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
     case R.id.satelit: 
      googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
      return true; 

     case R.id.street: 
      googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      return true; 

     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 
} 

我想,如果用戶觸摸從我的位置標記,使一個路徑,我怎麼做呢?

+0

你想使用谷歌瀏覽器或你想要做到這一點自定義我可以幫助你與谷歌導航? – JRowan

+1

你有什麼嘗試?研究不起作用?告訴我,真的不是一個好問題 –

+0

是啊,這是嘿,使這個程序對我來說 – JRowan

回答

0

這就是我如何做到這一點:我聽說它只適用於美國多數民衆贊成我剛剛聽到的:腳,車,騎自行車,在那裏還分享到Facebook和方法堆疊您的地圖覆蓋您的歡迎使用它,我覺得有些進口是unnecesarry,但我還沒有來得及刪除它們,我也知道你的新,他們會踢你,如果你不表現出的研究工作,使您的問題少模糊

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.AlertDialog; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.graphics.drawable.Drawable; 
import android.net.Uri; 
import android.util.Log; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.OverlayItem; 

public class OverlayPoints extends ItemizedOverlay<OverlayItem> 
{ 

private ArrayList<Drawable> mImage = new ArrayList<Drawable>(); 
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
private ArrayList<File> filepic = new ArrayList<File>(); 

private Context mContext; 
private boolean active; 
private boolean touch = false; 


public OverlayPoints(Drawable defaultMarker, Context context) 
{ 
    super(boundCenterBottom(defaultMarker)); 
    mContext = context; 
    active = false; 

    // TODO Auto-generated constructor stub 
} 

public void addOverlay(OverlayItem overlay, Drawable image,File pic) 
{ 
    active = true; 

    filepic.add(pic); 
mOverlays.add(overlay); 
mImage.add(image); 
populate(); 

} 

@Override 
protected OverlayItem createItem(int arg0) 
{ 
    return mOverlays.get(arg0); 
    // TODO Auto-generated method stub 

} 
public void delete() 
{ 

    mImage = new ArrayList<Drawable>(); 
    mOverlays = new ArrayList<OverlayItem>(); 
} 

@Override 
public int size() { 
    // TODO Auto-generated method stub 
    return mOverlays.size(); 
} 
protected boolean onTap(int index) 
{ 
final OverlayItem item = mOverlays.get(index); 
final File file = filepic.get(index); 
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
dialog.setIcon(mImage.get(index)); 
dialog.setTitle(item.getTitle()); 
dialog.setMessage(item.getSnippet()); 
dialog.setNegativeButton("Dismiss", new OnClickListener(){ 

    @Override 
    public void onClick(DialogInterface arg0, int arg1) { 
     // TODO Auto-generated method stub 

    } 

}); 
dialog.setNeutralButton("Set Course", new OnClickListener(){ 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     GeoPoint point = item.getPoint(); 
     final double lat = (double)point.getLatitudeE6()/ 1E6; 
     final double lon = (double)point.getLongitudeE6()/ 1E6; 

     AlertDialog.Builder course = new AlertDialog.Builder(mContext); 
     course.setNegativeButton("On Foot", new OnClickListener(){ 


      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
       // TODO Auto-generated method stub 
       Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s", lat, lon, "&mode=w"))); 
       mContext.startActivity(i); 
      } 

     }); 

     course.setNeutralButton("By Car", new OnClickListener(){ 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s", lat, lon, "&mode=d"))); 
       mContext.startActivity(i); 
      } 

     }); 
     course.setPositiveButton("On Bike", new OnClickListener(){ 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s", lat, lon, "&mode=b"))); 
       mContext.startActivity(i); 
      } 

     }); 
    course.show(); 
    } 

}); 
dialog.setPositiveButton("Share", new OnClickListener(){ 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     Uri uri = Uri.fromFile(file); 
     Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); 

     shareIntent.setType("image/png"); 
     shareIntent.putExtra(android.content.Intent.EXTRA_STREAM, 
         uri); //Share the image on Facebook 
     PackageManager pm = mContext.getApplicationContext().getPackageManager(); 
     List<ResolveInfo> activityList = pm.queryIntentActivities(
          shareIntent, 0); 
     for (final ResolveInfo app : activityList) { 
      if ((app.activityInfo.name).contains("facebook")) { 
       final ActivityInfo activity = app.activityInfo; 
       final ComponentName name = new ComponentName(
          activity.applicationInfo.packageName, 
          activity.name); 
       shareIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
       shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
       shareIntent.setComponent(name); 
       mContext.startActivity(shareIntent); 
       break; 
        } 
       } 
    } 

}); 
if(touch) 
    dialog.show(); 
return true; 
} 
public boolean isused() 
{ 
    return active; 
} 
public void showlogfalse(){ 
    touch = false; 
} 
public void showlogtrue(){ 
    touch = true; 
} 

}

+0

謝謝@JRowan我會嘗試它.. – Basribaz

+0

沒問題,希望有幫助 – JRowan

相關問題