First create a project on google apis web console. And get the API KEY for v2 maps.
Include google-play-services_lib in your project.
1.MainActivity.java
1. activity_main.xml
Include google-play-services_lib in your project.
1.MainActivity.java
public class MainActivity extends FragmentActivity implements OnMapLongClickListener,OnMapClickListener,OnMarkerDragListener { private static GoogleMap map; ArrayListXML layouts :points; LatLng mLatlng; int i=0; float distance; @Override protected void onCreate(Bundle saveInstance) { super.onCreate(saveInstance); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); points = new ArrayList (); map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); map.setOnMarkerDragListener(this); map.setOnMapLongClickListener(this); map.setOnMapClickListener(this); mLatlng=new LatLng(30.7500, 76.7800 ); CameraPosition INIT = new CameraPosition.Builder() .target(new LatLng(30.7500, 76.7800)) .zoom(17.5F) .bearing(300F) // orientation .tilt( 50F) // viewing angle .build(); // use map to move camera into position map.moveCamera( CameraUpdateFactory.newCameraPosition(INIT) ); points.add(new LatLng(30.7500, 76.7800)); //create initial marker map.addMarker( new MarkerOptions() .position( new LatLng(30.7500, 76.7800) ) .title("Location").draggable(true) .snippet("First Marker")).showInfoWindow(); } @Override public void onMarkerDrag(Marker arg0) { LatLng dragPosition = arg0.getPosition(); double dragLat = dragPosition.latitude; double dragLong = dragPosition.longitude; Log.i("info", "on drag end :" + dragLat + " dragLong :" + dragLong); 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(dragPosition); // Setting points of polyline polylineOptions.addAll(points); // Adding the polyline to the map map.addPolyline(polylineOptions); Location locationA = new Location("point A"); locationA.setLatitude(mLatlng.latitude); locationA.setLongitude(mLatlng.longitude); Location locationB = new Location("point B"); locationB.setLatitude(dragLat); locationB.setLongitude(dragLong); distance = locationA.distanceTo(locationB) ; mLatlng=new LatLng(dragLat, dragLong); } @Override public void onMarkerDragEnd(Marker arg0) { LatLng dragPosition = arg0.getPosition(); double dragLat = dragPosition.latitude; double dragLong = dragPosition.longitude; Log.i("info", "on drag end :" + dragLat + " dragLong :" + dragLong); 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(dragPosition); // Setting points of polyline polylineOptions.addAll(points); // Adding the polyline to the map map.addPolyline(polylineOptions); Toast.makeText(getApplicationContext(), "Distance ="+distance, Toast.LENGTH_LONG).show(); } @Override public void onMarkerDragStart(Marker arg0) { } @Override public void onMapClick(LatLng arg0) { map.animateCamera(CameraUpdateFactory.newLatLng(arg0)); } @Override public void onMapLongClick(LatLng arg0) { } }
1. activity_main.xml
<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>2. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kamal" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <permission android:name="com.kamal.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.kamal.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-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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="com.kamal.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="AIzaSyAchnHfCbb35xPnFfd-dbQ72kQ7glqvVzg" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> </manifest>OutPut :
No comments:
Post a Comment