Develop an android Application that display Google map in android virtual device.

No comments
//AndroidManifest.xml

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

   
<!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
   
   
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

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

       
<!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->
       
<meta-data
           
android:name="com.google.android.geo.API_KEY"
           
android:value="@string/google_maps_key" />


        <
activity
           
android:name=".MapsActivity"
           
android:label="@string/title_activity_maps">
            <
intent-filter>
                <
action android:name="android.intent.action.MAIN" />

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

</
manifest>

//MapsActivity.java

package com.example.arpit.p12;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.appindexing.AndroidAppUri;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.vision.barcode.Barcode;

import java.io.IOException;
import java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener, GoogleMap.OnMapLongClickListener, GoogleMap.OnMapClickListener, GoogleMap.OnMarkerClickListener {

   
private GoogleMap mMap;
   
private final int[] MAP_TYPES = {GoogleMap.MAP_TYPE_NONE,
            GoogleMap.MAP_TYPE_NORMAL,
            GoogleMap.MAP_TYPE_SATELLITE,
            GoogleMap.MAP_TYPE_TERRAIN,
            GoogleMap.MAP_TYPE_HYBRID,
    };
   
private int curMapTypeIndex = 1;
    LatLng ll;
    EditText etsearch;

    @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.
       
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(
this);
        etsearch=(EditText)findViewById(R.id.edtsearch);

    }


   
/**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
   
@Override
   
public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(MAP_TYPES[curMapTypeIndex]);
        mMap.setTrafficEnabled(
true);
       
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED)
        {
           
// TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
           
return;
        }
        mMap.setMyLocationEnabled(
true);
        mMap.getUiSettings().setZoomControlsEnabled(
true );
       
// Add a marker in Sydney and move the camera
       
LatLng uvpce = new LatLng(23.526939, 72.459136);
        mMap.addMarker(
new MarkerOptions().position(uvpce).title("Marker in UVPCE").snippet("Welcome to uvpce"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(uvpce));
        mMap.setOnMapLongClickListener(
this);
        mMap.setOnInfoWindowClickListener(
this);
        mMap.setOnMapClickListener(
this);
    }

   
public void changeType(View view) {
        mMap.setMapType(MAP_TYPES[curMapTypeIndex]);
        Toast.makeText(
this, String.valueOf(MAP_TYPES[curMapTypeIndex]), Toast.LENGTH_SHORT).show();
        curMapTypeIndex++;
       
if(curMapTypeIndex>4)
        {
            curMapTypeIndex=
0;
        }
    }

    @Override
   
public void onInfoWindowClick(Marker marker) {
        Toast.makeText(
this, "info window click", Toast.LENGTH_SHORT).show();
    }

    @Override
   
public void onMapLongClick(LatLng latLng) {
        Toast.makeText(
this, "long click", Toast.LENGTH_SHORT).show();
        ll=latLng;
        MarkerOptions options =
new MarkerOptions().position(latLng);
        options.title(getAddressFromLatLng(latLng));

        options.icon( BitmapDescriptorFactory.defaultMarker() );
        mMap.addMarker(options);
    }

    @Override
   
public void onMapClick(LatLng latLng) {
        Toast.makeText(
this, "(lat:"+latLng.latitude+"-long:"+latLng.longitude+")", Toast.LENGTH_SHORT).show();

        ll=latLng;
    }

    @Override
   
public boolean onMarkerClick(Marker marker) {
        marker.showInfoWindow();
       
return false;
    }
   
////////////////////////////////////////
   
private String getAddressFromLatLng(LatLng latLng) {
        Geocoder geocoder =
new Geocoder(this);

        String address =
"";
       
try {
            address = geocoder
                    .getFromLocation(latLng.latitude,latLng.longitude,
1)
                    .get(
0).getAddressLine(0);
        }
       
catch (IOException e) {
        }

       
return address;
    }

   
public void circleClick(View view) {
       
// mMap.clear();
       
CircleOptions options = new CircleOptions();
        options.center(ll);
       
//Radius in meters
       
options.radius(10);
        options.fillColor(Color.argb(
125,0,0,120));
        options.strokeColor(Color.RED);
        options.strokeWidth(
10);
        mMap.addCircle(options);
    }

   
public void polyClick(View view) {
       
//  mMap.clear();
       
LatLng point2 = new LatLng(ll.latitude+0.001,ll.longitude);
        LatLng point3 =
new LatLng( ll.latitude,ll.longitude+0.001 );

        PolygonOptions options =
new PolygonOptions();
        options.add(ll,point2,point3);

        options.fillColor(Color.YELLOW);
        options.strokeColor(Color.GREEN);
        options.strokeWidth(
10);
        mMap.addPolygon( options );
    }

   
public void overClick(View view) {
       
//  mMap.clear();
       
GroundOverlayOptions options = new GroundOverlayOptions();
        options.position(ll,
200,200);

        options.image(BitmapDescriptorFactory
                .fromBitmap(BitmapFactory
                        .decodeResource(getResources(),R.mipmap.ic_launcher)));

        mMap.addGroundOverlay( options );
    }

   
public void searchClick(View view) {
        mMap.clear();
        Geocoder geocoder =
new Geocoder(this);
        List<Address> lst;
       
try {

            lst = geocoder.getFromLocationName(etsearch.getText().toString(),
1);
           
double lat=lst.get(0).getLatitude();
           
double lon=lst.get(0).getLongitude();
            LatLng sp=
new LatLng(lat,lon);
            mMap.addMarker(
new MarkerOptions().position(sp));
           
//mMap.moveCamera(CameraUpdateFactory.newLatLng(sp));
            
CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(sp)     
// Sets the center of the map to Mountain View
                   
.zoom(17)                   // Sets the zoom
                   
.bearing(90)                // Sets the orientation of the camera to east
                   
.tilt(30)                   // Sets the tilt of the camera to 30 degrees
                   
.build();
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
catch (IOException e) {
        }
    }


   
///////////////////////////////////
}
//activity_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_height="match_parent"
   
android:layout_width="match_parent">

    <
LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:orientation="vertical"
       
tools:ignore="UselessParent">
        <
LinearLayout
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:orientation="horizontal"
           
>
            <
EditText
               
android:layout_width="280dp"
                
android:layout_height="wrap_content"
               
android:id="@+id/edtsearch"
               
tools:ignore="TextFields" />
            <
Button
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="search"
               
android:onClick="searchClick"
               
tools:ignore="HardcodedText" />
        </
LinearLayout>

        <
fragment xmlns:android="http://schemas.android.com/apk/res/android"
            
xmlns:map="http://schemas.android.com/apk/res-auto"
           
xmlns:tools="http://schemas.android.com/tools"
           
android:id="@+id/map"
           
android:name="com.google.android.gms.maps.SupportMapFragment"
           
android:layout_width="match_parent"
           
android:layout_height="420dp"
           
tools:context="com.example.arpit.p12.MapsActivity"
           
android:layout_alignParentTop="true"
           
android:layout_alignParentStart="true"
           
tools:ignore="ObsoleteLayoutParam"
           
android:layout_alignParentLeft="true" />

        <
LinearLayout
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:orientation="horizontal"
           
>
            <
Button
               
android:id="@+id/btn_type"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:onClick="changeType"
               
android:text="Type"
                
tools:ignore="ButtonStyle,HardcodedText" />

            <
Button
               
android:id="@+id/btn_circle"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:onClick="circleClick"
               
android:text="Circle"
               
tools:ignore="ButtonStyle,HardcodedText" />

            <
Button
               
android:id="@+id/btnPoly"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:onClick="polyClick"
               
android:text="Poly"
               
tools:ignore="ButtonStyle,HardcodedText" />

            <
Button
               
android:id="@+id/btnOverlay"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:onClick="overClick"
               
android:text="Overlay"
               
tools:ignore="ButtonStyle,HardcodedText" />
        </
LinearLayout>
    </
LinearLayout>
</
RelativeLayout>

//strings.xml

<resources>
    <
string name="app_name">p12</string>
    <
string name="title_activity_maps">Map</string>
</
resources>

//google_maps_api.xml


<resources>
   
<!--
   
TODO: Before you run your application, you need a Google Maps API key.

    To get one, follow this link, follow the directions and press "Create" at the end:

    https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=A1:43:45:7D:F9:40:32:8A:2A:81:02:BC:6B:85:19:E7:39:22:50:0E%3Bcom.example.arpit.p12

    You can also add your credentials to an existing key, using this line:
    A1:43:45:7D:F9:40:32:8A:2A:81:02:BC:6B:85:19:E7:39:22:50:0E;com.example.arpit.p12

    Alternatively, follow the directions here:
    https://developers.google.com/maps/documentation/android/start#get-key

    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
    string in this file.
    -->
   
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDPlfuJpE1OWNx99DuRwY18ZBBSUOsoCXA</string>
</
resources>

OUTPUT:-
 

No comments :

Post a Comment