Google Map API/Sdk for Android Application
Google Map Server have an API or Maps SDK for Android applications. API includes add maps, map display, add markers, polylines, polygons based on Google Maps data to our application from Google Maps servers. It have a auto listener like map gestures (Zooming and Userview of Map) and two other listeners onMapReadyCallBack and onMapClickListener. We can customize the basic map user's view of a particular map area.
Register Project and Get API key
Register for Google Developer Console , register your project as Android and get an API key for that project. Copy it and place it in string.xml
AndroidManifest.xml
|
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codeexpo.samplemap">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/api_key" />
<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>
|
MapsActivity.java
|
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng codeexpo = new LatLng(33.6504, 73.0922);
mMap.addMarker(new MarkerOptions().position(codeexpo).title("Code Expo"));
//Move Camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(codeexpo));
//Move Camera with Zoom
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(codeexpo,10));
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
@Override
public void onMapClick(LatLng point) {
Log.e(String.valueOf(point.latitude),String.valueOf(point.longitude));
}
});
}
});
}
}
|
string.xml
|
<resources>
<string name="app_name">Sample map</string>
<string name="title_activity_maps">Map</string>
<string name="api_key"></string>
</resources>
|
activity_maps.xml
|
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
|
Comments
Post a Comment