Google Map



Develop Android Google Map application

Step 1. Create a new Project.
Step 2. Go to the design view and select the Text Fields section in the Palette and locate Plain Text
Step 3. Drag a Button from the Form Widgets section into the layout and design a GUI as given below.

            GoogleMaps.jpg

Step 4.   Switch to the Text view, XML layout file and verify that the file looks similar to the following listing.
<LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical">

<LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">
          <EditText
          android:layout_width="280dp"
          android:layout_height="match_parent"
          android:id="@+id/EditTxtLoc"/>
          <Button
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="Search"
          android:id="@+id/BtnSrch"
          android:onClick="BtnSechClickd"/>

</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="463dp"
          android:layout_height="554dp"
          tools:context="idamithw.mymap.MapsActivity" />
</LinearLayout>

Step 5. Change your TestDatabaseActivity class by adding ListActivity to display the information on the view.

import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

          private GoogleMap mMap;
          Geocoder gc;
          TextView txtv;

          List<Address> addressList =null;

          LatLng latLang;

          @Override
         
protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_maps);
          SupportMapFragment mapFragment = (SupportMapFragment)  
getSupportFragmentManager().findFragmentById(R.id.map);
          mapFragment.getMapAsync(this);

          }

          @Override
         
public void onMapReady(GoogleMap googleMap) {
                   mMap = googleMap;
                  
mMap.setMyLocationEnabled(true);
          }

          public void BtnSechClickd(View view) {
                   txtv=(TextView)findViewById(R.id.EditTxtLoc);
                   String location = txtv.getText().toString();

          if(location != null || !location.equals(""))
          {
                   gc=new Geocoder(this);
          try {
                addressList = gc.getFromLocationName(location,1);
          }

          catch (IOException e) {
                   e.printStackTrace();
          }

          Address address= addressList.get(0);
          latLang = new LatLng (address.getLatitude() ,address.getLongitude() );
          mMap.addMarker(new MarkerOptions().position(latLang).title("Hear is the location"));
          mMap.animateCamera(CameraUpdateFactory.newLatLng(latLang));
          }
          }
}

Step 6.   Change your google_maps_api.xml file like this replace with your api key
 <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDHT84_ES5PKGKL9Ey03G2GbFvzVavfuV4</string>

Step 7.   Run your application on an Android device

Comments

Popular posts from this blog

SQL Lite Database Save

SQL Lite Database Save