Reputation: 107
I know that this question maybe was asked several times, but trust me, i tried every possibility and it doesn't work. I will be as briefly as possible.
.java
package com.example.aa_lbs;
import java.util.Calendar;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import android.widget.Toast;
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 com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.MapFragment;
/**
* Demonstrate the use of GPS within Android. Google Maps is optional for this
* application, but is useful in visualizing the output. Google Maps does not
* run in the AVD.
*
* @author Alex Tushinsky ([email protected])
* @version 1.0
*/
public class LBSActivity extends Activity implements LocationListener {
private LocationManager oLoc;
private Location currentLoc;
private Location previousLoc;
private double totalDistance = 0;
private long eventStartTime = 0;
TextView txtOutput = null;
private GoogleMap oMap;
Marker oMark = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lbs);
// Criteria object is optional. Will return value representing "GPS"
Criteria oGPSSettings = new Criteria();
oGPSSettings.setAccuracy(Criteria.ACCURACY_FINE);
oGPSSettings.setSpeedRequired(true);
oGPSSettings.setAltitudeRequired(true);
oGPSSettings.setBearingRequired(false);
oGPSSettings.setCostAllowed(false);
oGPSSettings.setPowerRequirement(Criteria.POWER_MEDIUM);
txtOutput = (TextView) findViewById(R.id.txtOutput);
oLoc = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = oLoc.getBestProvider(oGPSSettings, true);
if (provider != null) {
oLoc.requestLocationUpdates(provider, 1000, 1, this);
// or use LocationManager.GPS_PROVIDER instead of provider variable
} else {
Toast.makeText(getBaseContext(), "No GPS", Toast.LENGTH_SHORT)
.show();
finish();
}
eventStartTime = getRawTime();
}
@Override
public void onLocationChanged(Location location) {
currentLoc = location;
long currentTime = getRawTime();
if (previousLoc != null) {
// Calculates the distance between the last location and the
// current.
double dis = getDistance(previousLoc, currentLoc);
// Adds the distance to the previous total.
totalDistance += dis; // in KM
long totalTimeDiff = (currentTime - eventStartTime) / 1000L;
double CurrentMetersPerSecond = location.getSpeed();
double kps = totalTimeDiff / totalDistance; // km per second
// 1 kilometer per second = 3600 kilometers per hour or kps / 60
// minutes / 60 seconds
double kPH = 1 / ((kps / 60) / 60);
String sText = "Distance: "
+ String.format("%.2f", Double.valueOf(totalDistance))
+ " km\n" + "Speed: "
+ String.format("%.2f", (CurrentMetersPerSecond * 3.6))
+ " kph\n" + "Avg Speed: " + String.format("%.2f", (kPH))
+ " kph\n" + "Lon: " + currentLoc.getLongitude() + "\n"
+ "Lat: " + currentLoc.getLatitude() + "\n" + "Alt: "
+ currentLoc.getAltitude();
txtOutput.setText(sText);
}
// sets this location as last location.
previousLoc = currentLoc;
LatLng oPos = new LatLng(currentLoc.getLatitude(),
currentLoc.getLongitude());
oMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
if (oMap != null) {
oMap.clear(); // otherwise old markers remain
oMark = oMap.addMarker(new MarkerOptions().position(oPos).title(
"My Location"));
oMap.moveCamera(CameraUpdateFactory.newLatLngZoom(oPos, 17));
}
}
@Override
public void onProviderDisabled(String provider) {
oLoc.removeUpdates(this);
Toast.makeText(getBaseContext(), provider + " is disabled.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
oLoc.removeUpdates(this);
oLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
Toast.makeText(getBaseContext(), provider + " is enabled.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
oLoc.removeUpdates(this);
oLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
}
@Override
protected void onResume() {
super.onResume();
oLoc.removeUpdates(this);
oLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
}
@Override
protected void onPause() {
super.onPause();
oLoc.removeUpdates(this);
}
/**
*
* Returns the current date/time in milliseconds since epoch.
*/
public static long getRawTime() {
Calendar dt = Calendar.getInstance();
return dt.getTimeInMillis();
}
/**
* getDistance is used to return the distance in kilometers between two
* location points.
*/
public static double getDistance(Location PreviousLocation,
Location CurrentLocation) { // in KM
double lat1 = PreviousLocation.getLatitude();
double lon1 = PreviousLocation.getLongitude();
double lat2 = CurrentLocation.getLatitude();
double lon2 = CurrentLocation.getLongitude();
double latA = Math.toRadians(lat1);
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2);
double lonB = Math.toRadians(lon2);
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math
.cos(lonB - lonA)) + (Math.sin(latA) * Math.sin(latB));
double ang = Math.acos(cosAng);
double dist = ang * 6371; // earth's radius!
return dist;
}
}
.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="105dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtOutput"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa_lbs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<permission
android:name="com.example.aa_lbs.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.aa_lbs.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.aa_lbs.LBSActivity"
android:label="@string/app_name" >
<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="AIzaSyDR3gdB6hail53aWqO9CCaU1Mz3L3719_Y" />
</application>
</manifest>
My Virtual Machine
The Error:
04-18 11:04:20.217: E/AndroidRuntime(1242): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aa_lbs/com.example.aa_lbs.LBSActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
My app crash when it starts. I know the problem is with the fragments, but i don't find it! PLEASE! Help me, I give up...I'd appreciate forever.
Thank you so much.
Upvotes: 0
Views: 248
Reputation: 5595
Add the following,
< meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" >
< /meta-data>
in your manifest file immediately below the first meta-data tag(where you have placed your google api key)
Upvotes: 1