Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Draw path between two locations

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 260
    Comment on it

    In the below example I have created Drawing Polyline and Markers along the tapped position in Google Map. Polyline is a list of points where line segment are drawn between consecutive point. Here I have created fragment attribute in activity_main.xml and In AndroidMaindest.xml I have added permisson, In MainActivity I have used onMapClick method and OnClickListener method. You can see below example code it will clearly described you how to get Draw path between two locations.

    Step(1)activity_main.xml layout-

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment"/>
    
    </RelativeLayout>
    

    Step(2)AndroidManifest.xml-

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       
        android:versionCode="1"
        android:versionName="1.0" >
     
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
     
        <permission
            android:name="in.wptrafficanalyzer.locationpolyline.permission.MAPS_RECEIVE"
            android:protectionLevel="signature"/>
     
        <uses-permission android:name="in.wptrafficanalyzer.locationpolyline.permission.MAPS_RECEIVE"/>
     
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
     
        <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" >
     
            <activity
                android:name="in.wptrafficanalyzer.locationpolyline.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>
     
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="YOUR_API_KEY"/>
     
        </application>
    </manifest>
    

    Step(3)MainActivity-

    public class MainActivity extends FragmentActivity {
    
        GoogleMap googleMap;
        ArrayList<LatLng> points;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            points = new ArrayList<LatLng>();
    
            // 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);
    
            // Setting OnClick event listener for the Google Map
            googleMap.setOnMapClickListener(new OnMapClickListener() {
    
                @Override
                public void onMapClick(LatLng point) {
    
                    // Instantiating the class MarkerOptions to plot marker on the map
                    MarkerOptions markerOptions = new MarkerOptions();
    
                    // Setting latitude and longitude of the marker position
                    markerOptions.position(point);
    
                    // Setting titile of the infowindow of the marker
                    markerOptions.title("Position");
    
                    // Setting the content of the infowindow of the marker
                    markerOptions.snippet("Latitude:"+point.latitude+","+"Longitude:"+point.longitude);
    
                    // Instantiating the class PolylineOptions to plot polyline in the map
                    PolylineOptions polylineOptions = new PolylineOptions();
    
                    // Setting the color of the polyline
                    polylineOptions.color(Color.RED);
    
                    // Setting the width of the polyline
                    polylineOptions.width(3);
    
                    // Adding the taped point to the ArrayList
                    points.add(point);
    
                    // Setting points of polyline
                    polylineOptions.addAll(points);
    
                    // Adding the polyline to the map
                    googleMap.addPolyline(polylineOptions);
    
                    // Adding the marker to the map
                    googleMap.addMarker(markerOptions);
    
                }
            });
    
            googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
    
                @Override
                public void onMapLongClick(LatLng point) {
                    // Clearing the markers and polylines in the google map
                    googleMap.clear();
    
                    // Empty the array list
                    points.clear();
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: