Create Map in android

//--------------------------------------------Code Detail and Working---------------------------------------------------------

Step 1 Dependances

//============================java Code Start =========================================

implementation 'com.google.android.gms:play-services-maps:17.0.0'

=========================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

Step 2 add Google Console Key 

//============================java Code Start =========================================

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

=========================================End==========================================

//--------------------------------------------Code Detail and Working---------------------------------------------------------

Step 3 add permission

//============================java Code Start =========================================

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

=========================================End==========================================

Step 4 add design show map

//============================java Code Start =========================================

<fragment

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:id="@+id/map"

       android:name="com.google.android.gms.maps.MapFragment" />

=========================================End==========================================

Step 5 add code java file

//============================java Code Start =========================================

public class MainActivity extends AppCompatActivity {

//create map Variable
    GoogleMap
map;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_main);

//Map show fragment show
       
MapFragment mapFragment=(MapFragment) getFragmentManager().findFragmentById(R.id.map);

//method create getMapAsync
       
mapFragment.getMapAsync(new OnMapReadyCallback() {
           
@Override
           
public void onMapReady(@NonNull GoogleMap googleMap) {

                                           //save map variable
               
map=googleMap;

                                           //location set
               
LatLng sixroad=new LatLng(33.626057,73.071442);

//add marker
               
map.addMarker(new MarkerOptions().position(sixroad).title("Marker in 6th Road"));

//move location
               
map.moveCamera(CameraUpdateFactory.newLatLng(sixroad));
          
//zoom location     map.animateCamera(CameraUpdateFactory.newLatLngZoom(sixroad, 15));

//Long press add Marker
               
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
                   
@Override
                   
public void onMapLongClick(@NonNull LatLng latLng) {
     
//add marker onMapLongClick                 

map.addMarker(new MarkerOptions().position(latLng));
                   
}
                })
;
           
}
        })
;
   
}

=========================================End==========================================

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.