Step 1 :- Create New Android Project.
Step 2 :- Add Google-play-services_lib to your project.
Step 3 :- Open AndroidManifest.xml file.
<!--?xml version="1.0" encoding="utf-8"?--> package="com.mapv2.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <permission android:name="com.mapv2.demo.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <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.mapv2.demo.MainActivity" android:label="@string/app_name" > <activity android:name="com.mapv2.demo.MapDemoActivity" android:label="@string/app_name" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyCmX7SLVHXxU9pSqb2QbAOvdnjAGUulOrk" />
Step 4 :- Open activity_main.xml.
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/et_lat" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal|numberSigned" android:hint="@string/hnt_lat" /> <EditText android:id="@+id/et_lng" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="numberDecimal|numberSigned" android:layout_below="@id/et_lat" android:hint="@string/hnt_lng" /> <Button android:id="@+id/btn_show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_btn_show" android:layout_below="@id/et_lng" />
1.) activity_map.xml
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" />
Step 5 :- Open MainActivity.java
package com.mapv2.demo; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Getting reference to button btn_show Button btnShow = (Button) findViewById(R.id.btn_show); // Setting click event listener for the button btnShow.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { double lat=0; double lng=0; // Getting reference to EditText et_lat EditText etLat = (EditText) findViewById(R.id.et_lat); // Validating User Input if(etLat.getText().toString().isEmpty()){ Toast.makeText(getBaseContext(), "Please Enter Latitude", Toast.LENGTH_SHORT).show(); // Focus the EditText for Latitude etLat.requestFocus(); return; }else{ // Getting user input latitude lat = Double.parseDouble(etLat.getText().toString()); } // Getting reference to EditText et_lng EditText etLng = (EditText) findViewById(R.id.et_lng); // Validating User Input if(etLng.getText().toString().isEmpty()){ Toast.makeText(getBaseContext(), "Please Enter Longitude", Toast.LENGTH_SHORT).show(); // Focus the EditText for Longitude etLng.requestFocus(); return; }else{ // Getting user input longitude lng = Double.parseDouble(etLng.getText().toString()); } Intent intent = new Intent(getBaseContext(), MapDemoActivity.class); // Passing latitude and longitude to the MapActiv intent.putExtra("lat", lat); intent.putExtra("lng", lng); startActivity(intent); } }); } @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; } }
1.) MapDemoActivity.java
package com.mapv2.demo; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; 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.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MapDemoActivity extends FragmentActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); // Receiving latitude from MainActivity screen double latitude = getIntent().getDoubleExtra("lat", 0); // Receiving longitude from MainActivity screen double longitude = getIntent().getDoubleExtra("lng", 0); LatLng position = new LatLng(latitude, longitude); // Instantiating MarkerOptions class MarkerOptions options = new MarkerOptions(); // Setting position for the MarkerOptions options.position(position); // Setting title for the MarkerOptions options.title("Position"); // Setting snippet for the MarkerOptions options.snippet("Latitude:"+latitude+",Longitude:"+longitude); // Getting Reference to SupportMapFragment of activity_map.xml SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); // Getting reference to google map GoogleMap googleMap = fm.getMap(); // Adding Marker on the Google Map googleMap.addMarker(options); // Creating CameraUpdate object for position CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position); // Creating CameraUpdate object for zoom CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(4); // Updating the camera position to the user input latitude and longitude googleMap.moveCamera(updatePosition); // Applying zoom to the marker position googleMap.animateCamera(updateZoom); } @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; } }
Step 6 :- Run Code.
Leave a Reply