簡単な位置追跡アプリを作成しようとしていますが、多くの方法を試しましたが、何も機能しません。 onLocationChangedが呼び出されていないと思います。
これが私のコードです:
@Override
public void onLocationChanged(Location location) {
Log.d("test", "onLocationChanged Called");
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
gMap.addMarker(new MarkerOptions().position(latLng));
gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.animateCamera(CameraUpdateFactory.zoomTo(15));
Log.d("location", "Latitude:" + latitude + ", Longitude:" + longitude);
}
これがアプリを実行するための私のコード全体です。新しいプロジェクトを作成してマッププロジェクトを選択し、Googleコンソールでプロジェクトを作成して、プロジェクトにキーを追加するだけです。
マニフェストに権限を追加します:
<uses-permission Android:name="Android.permission.ACCESS_FINE_LOCATION"/>
Javaコード:
package com.bluebirds.avinash.uberdemo;
import Android.Manifest;
import Android.content.pm.PackageManager;
import Android.graphics.Point;
import Android.location.Location;
import Android.os.Bundle;
import Android.os.Handler;
import Android.os.Message;
import Android.os.SystemClock;
import Android.support.annotation.NonNull;
import Android.support.annotation.Nullable;
import Android.support.v4.app.ActivityCompat;
import Android.support.v4.app.FragmentActivity;
import Android.support.v4.content.ContextCompat;
import Android.util.Log;
import Android.view.animation.Interpolator;
import Android.view.animation.LinearInterpolator;
import Android.widget.Button;
import Android.widget.TextView;
import com.google.Android.gms.common.ConnectionResult;
import com.google.Android.gms.common.api.GoogleApiClient;
import com.google.Android.gms.common.api.PendingResult;
import com.google.Android.gms.common.api.Status;
import com.google.Android.gms.location.LocationListener;
import com.google.Android.gms.location.LocationRequest;
import com.google.Android.gms.location.LocationServices;
import com.google.Android.gms.maps.CameraUpdateFactory;
import com.google.Android.gms.maps.GoogleMap;
import com.google.Android.gms.maps.OnMapReadyCallback;
import com.google.Android.gms.maps.Projection;
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;
public class MapsActivity extends FragmentActivity implements LocationListener,
OnMapReadyCallback, GoogleApiClient
.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
private final int MY_LOCATION_REQUEST_CODE = 100;
private Handler handler;
private Marker m;
// private GoogleApiClient googleApiClient;
public final static int SENDING = 1;
public final static int CONNECTING = 2;
public final static int ERROR = 3;
public final static int SENT = 4;
public final static int SHUTDOWN = 5;
private static final String TAG = "LocationActivity";
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;
Button btnFusedLocation;
TextView tvLocation;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
String mLastUpdateTime;
private Location previousLocation;
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SENDING:
break;
}
}
};
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
m = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in " +
"Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
}
}
public void rotateMarker(final Marker marker, final float toRotation, final float st) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = st;
final long duration = 1555;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed / duration);
float rot = t * toRotation + (1 - t) * startRotation;
marker.setRotation(-rot > 180 ? rot / 2 : rot);
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}
public void animateMarker(final LatLng toPosition, final boolean hideMarke) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = mMap.getProjection();
Point startPoint = proj.toScreenLocation(m.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 5000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * toPosition.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startLatLng.latitude;
m.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarke) {
m.setVisible(false);
} else {
m.setVisible(true);
}
}
}
});
}
private double bearingBetweenLocations(LatLng latLng1, LatLng latLng2) {
double PI = 3.14159;
double lat1 = latLng1.latitude * PI / 180;
double long1 = latLng1.longitude * PI / 180;
double lat2 = latLng2.latitude * PI / 180;
double long2 = latLng2.longitude * PI / 180;
double dLon = (long2 - long1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
* Math.cos(lat2) * Math.cos(dLon);
double brng = Math.atan2(y, x);
brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
return brng;
}
/* @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
if (requestCode == MY_LOCATION_REQUEST_CODE) {
if (permissions.length == 1 &&
permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Permission was denied. Display an error message.
}
}
}*/
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart fired ..............");
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected());
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
protected void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi
.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Log.d(TAG, "Location update started ..............: ");
}
LatLng previouslatLng;
@Override
public void onLocationChanged(Location location) {
previouslatLng = new LatLng(location.getLatitude(), location.getLongitude());
double rota = 0.0;
double startrota = 0.0;
if (previousLocation != null) {
rota = bearingBetweenLocations(previouslatLng, new LatLng(location.getLatitude
(), location.getLongitude()));
}
rotateMarker(m, (float) rota, (float) startrota);
previousLocation = location;
Log.d(TAG, "Firing onLocationChanged..........................");
Log.d(TAG, "lat :" + location.getLatitude() + "long :" + location.getLongitude());
Log.d(TAG, "bearing :" + location.getBearing());
animateMarker(new LatLng(location.getLatitude(), location.getLongitude()), false);
// new ServerConnAsync(handler, MapsActivity.this,location).execute();
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
Log.d(TAG, "Location update stopped .......................");
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected()) {
startLocationUpdates();
Log.d(TAG, "Location update resumed .....................");
}
}
}
XMLアクティビティ:
<fragment Android:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
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:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context="com.bluebirds.avinash.uberdemo.MapsActivity"/>
このコードを使用すると、開始位置と終了位置を追跡でき、マーカーは開始位置から終了位置に移動します。
1。 Mainfestを追加
<uses-permission Android:name="Android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission Android:name="Android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission Android:name="Android.permission.INTERNET"/>
<meta-data
Android:name="com.google.Android.maps.v2.API_KEY"
Android:value="Your string" />
2。 Gradleで
compile 'com.google.Android.gms:play-services:7.0.0'
3。活動を見つける、
public class FindActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
private GoogleMap mMap;
public Toolbar toolbar;
double latitude, logitude,prevLatitude,prevLongitude;
Button button;
GPSTracker3 gps;
String work="0";
TextView textView, t1, t2, t3;
Timer timer = new Timer();
private List<LatLng> bangaloreRoute;
ArrayList<LatLng> MarkerPoints;
Context mContext;
Button b1, b2;
CardView change;
List<LatLng> list;
Location location;
private static final CharSequence[] MAP_TYPE_ITEMS = {"Road Map", "Hybrid", "Satellite", "Terrain"};
Marker marker;
private Polyline line;
private ArrayList<LatLng> routePoints ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
list = new ArrayList<>();
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mContext = this;
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(FindActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
// Toast.makeText(mContext,"You need have granted permission",Toast.LENGTH_SHORT).show();
gps = new GPSTracker3(mContext, FindActivity.this);
// Check if GPS enabled
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
logitude = gps.getLongitude();
// \n is for new line
// Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + lati + "\nLong: " + longi, Toast.LENGTH_LONG).show();
} else {
// Can't get location.
// GPS or network is not enabled.
// Ask user to enable GPS/network in settings.
// gps.showSettingsAlert();
}
}
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
MarkerPoints = new ArrayList<>();
b1 = (Button) findViewById(R.id.start);
b2 = (Button) findViewById(R.id.end);
change = (CardView)findViewById(R.id.locate);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
work = "1";
b1.setVisibility(View.GONE);
b2.setVisibility(View.VISIBLE);
// bangaloreRoute.add(new LatLng(latitude, logitude));
// showMessageLocationUpdate();
}
});
if (bangaloreRoute == null) {
bangaloreRoute = new ArrayList<>();
} else {
bangaloreRoute.clear();
}
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// bangaloreRoute.clear();
list.clear();
work = "2";
b2.setVisibility(View.GONE);
b1.setVisibility(View.VISIBLE);
CaptureMapScreen();
}
});
change.setBackgroundResource(R.drawable.ic_sate);
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showMapTypeSelectorDialog();
}
});
}
public void CaptureMapScreen()
{
GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
// TODO Auto-generated method stub
bitmap = snapshot;
try {
FileOutputStream out = new FileOutputStream("/mnt/sdcard/"
+ "MyMapScreen" + System.currentTimeMillis()
+ ".png");
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
};
mMap.snapshot(callback);
}
private void showMessageLocationUpdate() {
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
StartCase(location);
}
});
}
}, 0, 100);
}
private void StartCase(Location location) {
if (work.equals("1")) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_Magenta));
mCurrLocationMarker = mMap.addMarker(markerOptions);
list.add(latLng);
drawPolyLineOnMap(list);
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(30));
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
if (Android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
// getAddress(location.getLatitude(), location.getLongitude());
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_Magenta));
mCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
if (work.equals("1")) {
list.add(latLng);
drawPolyLineOnMap(list);
}
}
private void showMapTypeSelectorDialog(){
final String title = "Select Map Type";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
int checkCurrentMapType = mMap.getMapType() - 1;
builder.setSingleChoiceItems(MAP_TYPE_ITEMS,checkCurrentMapType,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item){
switch(item){
case 1:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case 2:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case 3:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
default:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
dialog.dismiss();
}
});
//build dialog
AlertDialog fMapTypeDialogue = builder.create();
fMapTypeDialogue.setCanceledOnTouchOutside(true);
fMapTypeDialogue.show();
}
public void drawPolyLineOnMap(List<LatLng> list) {
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(Color.RED);
polyOptions.width(3);
polyOptions.addAll(list);
mMap.clear();
mMap.addPolyline(polyOptions);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng : list) {
builder.include(latLng);
}
// final LatLngBounds bounds = builder.build();
//BOUND_PADDING is an int to specify padding of bound.. try 100.
// CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 10);
// mMap.animateCamera(cu);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
String currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
double latitude = obj.getLatitude();
double longitude = obj.getLongitude();
String currentCity= obj.getSubAdminArea();
String currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
t1.setText(obj.getAdminArea());
t2.setText(obj.getSubAdminArea());
t3.setText(obj.getSubThoroughfare());
System.out.println("obj.getCountryName()"+obj.getCountryName());
System.out.println("obj.getCountryCode()"+obj.getCountryCode());
System.out.println("obj.getAdminArea()"+obj.getAdminArea());
System.out.println("obj.getPostalCode()"+obj.getPostalCode());
System.out.println("obj.getSubAdminArea()"+obj.getSubAdminArea());
System.out.println("obj.getLocality()"+obj.getLocality());
System.out.println("obj.getSubThoroughfare()"+obj.getSubThoroughfare());
Log.v("IGA", "Address" + add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
}
} else {
Toast.makeText(this, "permission denied",
Toast.LENGTH_LONG).show();
}
return;
}
}
}
}
試すことができます https://github.com/balwinderSingh1989/androidBestLocationTracker GPSまたはネットワークから最適な位置を取得する位置アルゴリズムとlibを統合する簡単な方法(利用可能なものは何でも)
これを参照プロジェクトとして使用してください: http://www.vogella.com/tutorials/AndroidLocationAPI/article.html