Working with Google Maps V2 : Nowadays we So many applications Using maps to show Current location, this Info is More useful identify the Exact location of particular shop or Office. In android application, we have to use Google Map Service for Loadmaps in this tutorial we learn about Google maps v2 and how to show circle, markers Requirements for Show maps in Android App : 1). Google play services 2). Google API key How To Import Google PlayServices in Android: Google Mad new Maps v2 API part in Google Play services SDK. before you start working with maps we have to import Play services on Application First Add following line on Gradel file
Step3:- Get API key from Console, by selecting a project
copy key from the Console application How To Display Google Maps in Android Application: Create New Application with a name of Googlemapsv2. Add Following code in Manifest implementation 'com.google.android.gms:play-services-maps:11.8.0'
How TO Get Google Maps API key :
Step1:-Open Google Developer Console url
Step2:- Create new Project
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.arula.googlemapsv2">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.truiton.supportmapfragment.permission.MAPS_RECEIVE" />
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR KEY" />
</application>
</manifest>
Add Following code To Mainactivity UI:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<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.imaadv.leaynik.ClinicFragment" />
</android.support.constraint.ConstraintLayout>
Add Following Code in Main activity :
package in.arula.googlemapsv2;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
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 googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initilizeMap();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
private void initilizeMap() {
if (googleMap == null) {
((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMapAsync(this);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap=googleMap;
Circle circle = googleMap.addCircle(new CircleOptions()
.center(new LatLng(-33.87365, 151.20689))
.radius(10000)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
LatLng sydney = new LatLng(-33.87365, 151.20689);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
googleMap.setMinZoomPreference(11);
}
}
0 comments:
Post a Comment