• Tutorials
  • Tips & Tricks
  • Applications
  • News

Android Trainee

  • Tutorials
  • Tips & Tricks
  • Applications
  • News
Home  /  Tutorials  /  Showing current location using OnMyLocationChangeListener in Google Map Android API V2
21 November 2014

Showing current location using OnMyLocationChangeListener in Google Map Android API V2

Written by admin@androidtrainee
Tutorials android, Android App, androidmapv2, google place api, location, map, mapv2, mapv2 api, touched location Leave a Comment

Step 1 :- Create New Android Project.

Step 2 :- Add Google-play-services_lib to your project.

Step 3 :- Open AndroidManifest.xml file.

<!--?xml version="1.0" encoding="utf-8"?-->
package="com.mapv2.demo"
android:versionCode="1"
android:versionName="1.0" &gt;

&lt;uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" /&gt;

&lt;permission
android:name="com.mapv2.demo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/&gt;

&nbsp;

&lt;uses-feature
android:glEsVersion="0x00020000"
android:required="true"/&gt;

&lt;application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" &gt;
&lt;activity
android:name="com.mapv2.demo.MainActivity"
android:label="@string/app_name" &gt;
&nbsp;

&lt;meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCmX7SLVHXxU9pSqb2QbAOvdnjAGUulOrk"/&gt;

&nbsp;

Step 4 :- Open activity_main.xml.

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tv_location"
class="com.google.android.gms.maps.SupportMapFragment"
/>

Step 5 :- Open MainActivity.java

package com.mapv2.demo;

import android.app.Dialog;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends FragmentActivity implements OnMyLocationChangeListener {

GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();

}else { // Google Play Services are available

// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

// Getting GoogleMap object from the fragment
googleMap = fm.getMap();

// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);

// Setting event handler for location change
googleMap.setOnMyLocationChangeListener(this);

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public void onMyLocationChange(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);

// Getting latitude of the current location
double latitude = location.getLatitude();

// Getting longitude of the current location
double longitude = location.getLongitude();

// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);

// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );

}
}

Step 6 :- Run Code.

admin@androidtrainee

 Previous Article Adding Marker at user input latitude and longitude in Google Map Android API V2
Next Article   Showing nearby places using Google Places API and Google Map Android API V2

Related Posts

  • Android New Quick Action Animation.

    July 15, 2015
  • Android satellite menu Animation.

    July 15, 2015
  • Android Staggered Grid & List View.

    July 14, 2015

Leave a Reply

Cancel reply

Tags

admob Advertising Networks AerServ Airpush android android ads android Advertising Networks Android App android chart animation Android GridView android L android lollipop androidmapv2 AppBrain AppFlood Appia AppKey Appnext AppOptim Appwiz chart chartview Epom Market google place api GridView Image Loader InMobi LeadBolt location map mapv2 mapv2 api material design Minimob Mobicow MobileCore MobiMicro NativeX Pingjam RevMob StarApplication startapp TapContext touched location Widdit

Count per Day

  • 347Reads yesterday:
  • 463835Total visitors:
  • 55Visitors today:
  • 2428Visitors per month:
  • 0Visitors currently online:
© Copyright 2014. Theme by BloomPixel.
Posting....