Monday, May 19, 2014

Android show current location with Map v2

Android show current location with Map v2

*This article about how to show  current location with Map v2 .
*Setting speed to animated  layout

Create xml layout with custom speed indicator, current location text , Map view.

main_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res/com.vehicle.mediawhitetracking"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    tools:context="com.vehicle.mediawhitetracking.MainActivity"
    tools:ignore="MergeRootFrame" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="150dip"
    android:layout_above="@+id/map"
    android:layout_alignLeft="@+id/map"
    android:layout_alignRight="@+id/map"
    android:background="@color/white" >

    <RelativeLayout
        android:id="@+id/view1"
        android:layout_width="140dp"
        android:layout_height="160dp"
        android:background="@color/speed_circle_background" >
         <com.vehicle.mediawhitetracking.HalfSeekabr
            android:id="@+id/volume_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:clickable="false"
            android:secondaryProgress="0"
           
            app:end_angle="270"
            app:pointer_alpha_ontouch="100"
            app:pointer_color="#0174DF"
            app:pointer_halo_color="#880174DF"
            app:start_angle="270"
            app:use_custom_radii="true" />

         <TextView
             android:id="@+id/speed_textview"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="@color/white"
             android:textStyle="bold"
             android:layout_centerHorizontal="true"
             android:layout_centerVertical="true"
            ></TextView>
    </RelativeLayout>

    <ImageView
        android:id="@+id/image_gps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/gps" />

    <TextView
        android:id="@+id/text_currentlocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="@dimen/text_size"
        android:layout_marginLeft="10dip"
        android:textColor="@color/current_location_textcolor"
        android:layout_toRightOf="@+id/view1"
        android:singleLine="true"
        android:text="@string/current_location" />
</RelativeLayout>

    <fragment
        android:id="@+id/map"
        android:layout_width="356dp"
        android:layout_height="340dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dip"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>


mainactivity.class
Activity having Location GPS listener and Set speed to custom speed indicator.

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

       GoogleMap gMap;
       HalfSeekabr halfSeekBar;
       LatLng latLang;
    TextView textView,currentLocationAdress;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              halfSeekBar = (HalfSeekabr) findViewById(R.id.volume_bar);
              textView = (TextView)findViewById(R.id.speed_textview);
              currentLocationAdress = (TextView)findViewById(R.id.text_currentlocation);
              SupportMapFragment supportedmapFragment = (SupportMapFragment) getSupportFragmentManager()
                           .findFragmentById(R.id.map);
              gMap = supportedmapFragment.getMap();
              gMap.setMyLocationEnabled(true);
              halfSeekBar.setMax(120);
              gMap.animateCamera(CameraUpdateFactory.zoomTo(12));
              gpsListener locationList = new gpsListener();
              LocationManager locationmanger = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
              locationmanger.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                           2000, 50, locationList);

       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {

              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     return true;
              }
              return super.onOptionsItemSelected(item);
       }

       //Gps Location Listener
      
       private class gpsListener implements android.location.LocationListener {

              @Override
              public void onLocationChanged(Location location) {

                    
                    
                     latLang = new LatLng(location.getLatitude(),
                                  location.getLongitude());
                     if (location != null) {
                           //Setting speed to Textview
                           textView.setText(""+75 +" km/h");
                           halfSeekBar.setProgress(75);
                           //Animate to current Location
                           gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLang,
                                         15));
                           Geocoder geocoder; 
                      List<Address> addresses;
                      geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
                       try {
                                  addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                                  currentLocationAdress.setText(addresses.get(0).getAdminArea() +","+addresses.get(0).getAddressLine(0));
                           } catch (IOException e) {
                                 
                                  e.printStackTrace();
                           }
                  
                     }
                    
              }

              @Override
              public void onProviderDisabled(String provider) {
                     // TODO Auto-generated method stub

              }

              @Override
              public void onProviderEnabled(String provider) {
                     // TODO Auto-generated method stub
             
              }

              @Override
              public void onStatusChanged(String provider, int status, Bundle extras) {
                     // TODO Auto-generated method stub

              }
       }

}




Add the Following Permission in manifest file and manifestfile


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vehicle.mediawhitetracking"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <permission
        android:name="com.vehicle.mediawhitetracking.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.vehicle.mediawhitetracking.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/Theme.Mw" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyADeOdxjKTffVwQBNjQwR-E72v7JzvRpcY" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.vehicle.mediawhitetracking.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>
    </application>

</manifest>



Download Source Code





No comments:

Post a Comment

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...