distance and travel time in google maps api versio

2019-07-19 16:30发布

问题:

I want to make an application to display travel time and distance as well as the path to the location of the user's location to the destination location.

Here is my TampilkanMap class:

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

I want to make a path if the user touches the marker from my location, how do I make it?

回答1:

this is how im doing it: I heard it only works in USA thats just what i heard: theres by foot, by car, and by bike, in there theres also share to facebook and methods to stack your map overlays your welcome to use it, i think some of the imports are unnecesarry but i havent had time to remove them, also i know your new they will kick you out if you dont show research effort and make your question less vague

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

}