如何查找从当前位置到设置标记的距离和行程路线

时间:2014-10-31 19:48:40

标签: android google-maps

我正在进行出租车票价计算的项目,显示我当前的位置,但我想计算距离,并显示使用纬度从当前位置到其他城市的旅行路线。 请建议n帮助...

Maps.java       package com.example.mainproject;

  import java.util.List;

import android.content.Context;
      import android.content.Intent;
  import android.graphics.drawable.Drawable;
  import android.location.Location;
 import android.location.LocationListener;
  import android.location.LocationManager;
      import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
   import android.util.Log;
 import android.widget.Toast;

import com.example.mainproject.R;
 import com.google.android.gms.maps.CameraUpdate;
  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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
    import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;


 public class Maps extends FragmentActivity implements LocationListener {
// Google Map
    private GoogleMap googleMap;
    private LocationManager locationManager;
    private static final long MIN_TIME = 400;
    private static final float MIN_DISTANCE = 1000;

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

        try {
            // Loading map
            initilizeMap();

            // Changing map type
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            // googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

            // Showing / hiding your current location
            googleMap.setMyLocationEnabled(true);

            // Enable / Disable zooming controls
            googleMap.getUiSettings().setZoomControlsEnabled(false);

            // Enable / Disable my location button
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);

            // Enable / Disable Compass icon
            googleMap.getUiSettings().setCompassEnabled(true);

            // Enable / Disable Rotate gesture
            googleMap.getUiSettings().setRotateGesturesEnabled(true);

            // Enable / Disable zooming functionality
            googleMap.getUiSettings().setZoomGesturesEnabled(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, 
       MIN_DISTANCE, this); //You can also use LocationManager.GPS_PROVIDER and
      LocationManager.PASSIVE_PROVIDER      

    }

    @Override
    public void onLocationChanged(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
        googleMap.animateCamera(cameraUpdate);
        locationManager.removeUpdates(this);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) { }

    @Override
    public void onProviderEnabled(String provider) { }

    @Override
    public void onProviderDisabled(String provider) { }


    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    /**
     * function to load map If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    }

map.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

 </RelativeLayout>

清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mainproject"
android:versionCode="1"
android:versionName="1.0" >

<permission
    android:name="com.example.mainproject.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.mainproject.permission.MAPS_RECEIVE" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<!-- Requires OpenGL ES version 2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />    

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name="com.example.mainproject.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

        <activity class=".selectcity" android:label="selectcity"

        android:name="com.example.mainproject.selectcity">            
        </activity>

        <activity class=".About" android:label="About" 

         android:name="com.example.mainproject.About">            
        </activity>

        <activity class=".entervalues" android:label="entervalues" 
         android:name="com.example.mainproject.entervalues">         
        </activity>

        <activity class=".exit" android:label="exit" android:name="com.example.mainproject.exit">            
        </activity>

        <activity class=".Maps" android:label="Maps" android:name="com.example.mainproject.Maps">
         </activity>

        <meta-data
              android:name="com.google.android.maps.v2.API_KEY"
              android:value="AIza***************`enter code here`"/>
        <meta-data android:name="com.google.android.gms.version"  
            android:value="@integer/google_play_services_version" />
        </application>

        </manifest>

1 个答案:

答案 0 :(得分:0)

要获得Google的指示和距离,您必须执行以下操作: 将此代码写在方向按钮的点击监听器上,或者您希望显示方向和距离的任何位置。

String uri = "http://maps.google.com/maps?saddr="
                            + source_latitude + "," + source_longitude + "&daddr="
                            + destination_latitude + ","
                            + destination_longitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse(uri));
            intent.setClassName("com.google.android.apps.maps",
                    "com.google.android.maps.MapsActivity");
            startActivity(intent);