Hello
first go over
this
Important classes\interface :
interface OnMapReadyCallback
FragmentActivity
- Activity that want to use the Fragment API
- API :
FragmentManager
- interface to interact with fragment objects inside an activity
- API :
- used to get fragment inside the activity using their resource id
- result can be casted to SupportMapFragment
Fragment
- basically it is a gui component which lives inside an Activity
- nice explaination is here
SupportMapFragment
GoogleMap
- Enables or disables the my-location layer.
- requires permission for either
ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
- setting the boolean to true will show the circle button and false will hide it. it will center the map to the device location when enabled and provided the location is available. (it is not always working for me , i can see the button but press on it cause some times nothing)
- reposition the camera according to CameraUpdate
- CameraUpdate can be CameraUpdateFactory.newLatLngZoom( new LatLng(32.811,34.981), 13) . so in this case the camera will zoom around this location. 13 is the value of the zoom ,recommended to be between 2- 21
- add marker to the map
- e.g. addMarker(new MarkerOptions()
.title("Haifa")
.snippet("Home Town")
.position(LatLng(32.811,34.981)));
will position a marker at 32.811,34.981. click on it and you will see the title and snippet
Sample Code
MainActivity.java
/**
* This shows how to create a simple activity with a map and a marker on the map.
*/
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Haifa.
*/
@Override
public void onMapReady(GoogleMap map) {
LatLng Haifa = new LatLng(32.811,34.981);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(Haifa, 13));
map.addMarker(new MarkerOptions()
.title("Haifa")
.snippet("Home Town")
.position(Haifa));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trygooglemaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- THIS KEY IS PER PACKAGE !!!! -->
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCgth2vzXi1r6AQzW87qNjU8dKbjxlZtYU"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
you can get the sample
here
References :
Nathan