Pada kesempatan kali ini kita akan membahas tentang membuat
aplikasi maps di android studio dengan menggunakan google maps activity, oke
tanpa berlama – lama mari kita praktekan.
Pertama kita buat project baru beri nama courseMapsActivity,
Lalu pilih phone tablet -> Google maps activity
Buka file google_maps_api.xml di dalam folder res->values
<resources>
<!--
TODO: Before you run your application, you need a Google Maps API key.
To get one, follow this link, follow the directions and press "Create" at the end:
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=6C:AD:1F:8A:00:E6:E1:C7:E2:ED:AC:6A:9C:A6:3C:4C:50:4B:8D:6A%3Bcom.example.webdev.coursemapsactivity
You can also add your credentials to an existing key, using this line:
6C:AD:1F:8A:00:E6:E1:C7:E2:ED:AC:6A:9C:A6:3C:4C:50:4B:8D:6A;com.example.webdev.coursemapsactivity
Alternatively, follow the directions here:
https://developers.google.com/maps/documentation/android/start#get-key
Once you have your key (it starts with "AIza"), replace the "google_maps_key"
string in this file.
-->
<string name="google_maps_key" translatable="false" templateMergeStrategy="preserve">
YOUR_KEY_HERE
</string>
</resources>
Lalu ganti tulisan your key here dengan kode api key yang
telah kita generate pada tutorial sebelumnya tentang cara mendapatkan googleapi key,
Dalam google maps activity secara otomatis menggenerate
script standart onCreate dan onMapready :
package com.example.webdev.coursemapsactivity;
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.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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in 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));
}
}
Sekarang kita coba run -> project
Bagaimana mudahkan cara membuat maps di android studio,
sekian pembahasan kita kali ini pada
pembahasan selanjutnya kita akan membahas
tentang initial state di map fragment.
top
ReplyDelete