[go: up one dir, main page]

0% found this document useful (0 votes)
6 views3 pages

Unit 2

mobile application development

Uploaded by

priya j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Unit 2

mobile application development

Uploaded by

priya j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Developing an Android app and want to display Google Maps, follow these

steps:

Step 1: Set up your project in Android Studio

1. Create a new Android project in Android Studio or open an existing one.


2. Add Google Maps dependency:
o Open the build.gradle (Module: app) file.
o Add the following dependency to the dependencies block:
Implementation 'com.google.android.gms:play-services-maps:18.1.0'

3. Enable Google Maps SDK:

 Go to the Google Cloud Console (https://console.cloud.google.com).


 Create a new project or select an existing one.
 Enable the Google Maps Android API for your project.
 Create an API key for your project by going to APIs & Services > Credentials and then
adding a new API key.

4. Add the API Key to your AndroidManifest.xml:

 Open your AndroidManifest.xml file and add the following permission and metadata:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:usesCleartextTraffic="true">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key"/>
<!-- Other app components -->
</application>
Make sure you replace @string/google_maps_key with your actual API key, or store it in a
resource file (like strings.xml) as recommended.
Step 2: Add the Google Map Fragment

1. In your activity_main.xml (or your layout file), add the Google Map Fragment:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

2. In your MainActivity.java or MainActivity.kt file, set up the map:


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;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {


private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the map fragment and notify when the map is ready.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker at a specific location (e.g., Sydney) and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

You might also like