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

TestGoogleMap

The document provides an overview of setting up a Google Maps application for Android, including necessary permissions, API key configuration, and layout files. It details the AndroidManifest.xml file setup, including location permissions and API key management, as well as the implementation of a MapsActivity class to display a specific location on the map. Additionally, it includes instructions for validating the API key and enabling the Maps SDK for Android/iOS in the Google Cloud Console.
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)
3 views3 pages

TestGoogleMap

The document provides an overview of setting up a Google Maps application for Android, including necessary permissions, API key configuration, and layout files. It details the AndroidManifest.xml file setup, including location permissions and API key management, as well as the implementation of a MapsActivity class to display a specific location on the map. Additionally, it includes instructions for validating the API key and enabling the Maps SDK for Android/iOS in the Google Cloud Console.
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

Google Map

File AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.communityuni.testgooglemap">

<!--
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" />

<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="AIzaSyATgRMFxQ1EDtvdYF80OZh53HtDpkHX6l0" />

<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>
Lưu ý: Phải đăng nhập email bằng tài khoản gmail
1. Kiểm tra đảm bảo API Key hợp lệ: Truy cập Google Cloud Console : https://console.cloud.google.com/
- Chọn Project đang dùng.
- Vào APIs & Services > Credentials và kiểm tra API Key: nếu chưa có thì nhấn vào Create Credentials
để tạo API Key (https://console.cloud.google.com/apis/credentials)
2. Kiểm tra bật Maps SDK for Android/iOS -> Enable (https://console.cloud.google.com/apis/library)
File Activity_main.xml

<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="match_parent"
tools:context="com.communityuni.testgooglemap.MapsActivity" />
File MapsActivity.java

package com.communityuni.testgooglemap;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@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);
}
/**
* 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;

// Đổi thành vĩ tuyến kinh tuyến của nhà bạn


LatLng nhatui = new LatLng(10.86232, 106.73369);
Marker marker=mMap.addMarker(
new MarkerOptions()
.position(nhatui)
.title("Nhà Tui ở xóm nghèo nhất HCM")
.snippet("29/40/7/27 đường 42, Khu Phố 22, Phường Hiệp Bình
Chánh, Thủ Đức, HCM"));
marker.showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(nhatui,15));
}
}// Đây là cách phổ biến để hiển thị một vị trí cụ thể trên bản đồ
Mức Zoom từ 1 → 21:

- 1: nhìn toàn cầu (rất xa)


- 15: tầm nhìn cấp đường phố
- 20: rất gần (cấp độ nhà/cửa)

You might also like