Tuesday 30 May 2017

Draw Android Map Poly Line

Manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<activity
    android:name=".activity.MapsActivity"
    android:label="@string/title_activity_maps" />

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />

Java:


public class MapsActivity extends BaseActivity {

    private GoogleMap googleMap;

    private double fromLatitude = 1.306444;
    private double fromLongitude = 103.851109;

    private double toLatitude = 1.329356;
    private double toLongitude = 103.877889;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        googleMap.getUiSettings().setMapToolbarEnabled(false);

        List<LatLng> points = Arrays.asList(new LatLng(fromLatitude, fromLongitude),
                new LatLng(toLatitude, toLongitude));

        PolygonOptions options = new PolygonOptions();
        options.addAll(points);
        options.fillColor(getResources().getColor(R.color.app_theme_color));
        options.strokeColor(getResources().getColor(R.color.app_theme_color));
        googleMap.addPolygon(options);

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(fromLatitude, fromLongitude))
                .title("You Are here")
                .snippet("Order placed"));
        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(toLatitude, toLongitude))
                .title("Destination Position")
                .snippet("Order placed"));

        midPoint(fromLatitude, fromLongitude, toLatitude, toLongitude);
    }

    public void midPoint(double lat1, double lon1, double lat2, double lon2) {

        double dLon = Math.toRadians(lon2 - lon1);

        //convert to radians
        lat1 = Math.toRadians(lat1);
        lat2 = Math.toRadians(lat2);
        lon1 = Math.toRadians(lon1);

        double Bx = Math.cos(lat2) * Math.cos(dLon);
        double By = Math.cos(lat2) * Math.sin(dLon);
        double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
        double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);

        //print out in degrees
        System.out.println(Math.toDegrees(lat3) + " " + Math.toDegrees(lon3));

        CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(Math.toDegrees(lat3), Math.toDegrees(lon3)));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(14);
        googleMap.moveCamera(center);
        googleMap.animateCamera(zoom);
    }

}



Xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:elevation="7dp"
    android:fitsSystemWindows="false"
    tools:context=".activity.MapsActivity">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">/>

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

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/editext_border_login"
            android:layout_margin="@dimen/margin10">

            <Button
                android:id="@+id/email_sign_in_button"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin20"
                android:background="@drawable/login_btn_selector"
                android:text="@string/action_view_direction"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textStyle="bold" />
        </RelativeLayout>
  </RelativeLayout>
</RelativeLayout>


Images:

Screen Shot:

No comments:

Post a Comment

Send Whatsapp Message via PHP Code

  How to Send and Receive Messages in WhatsApp using PHP In this tutorial you will see How to Send and Receive Messages in WhatsApp using PH...