使用谷歌地图v2更新位置查找器 [英] Location finder along with updates using google map v2

查看:183
本文介绍了使用谷歌地图v2更新位置查找器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚接触android,我试图开发一个android应用程序,其中包括跟踪用户位置的详细信息并在Google地图上显示它,并在地图更改时更新位置。我无法理解如何通过google maps v2获取此信息。他们提到改变依赖关系,但我无法找到在哪里可以解释这个用法,我用一个简单的例子来实现下面的链接给出的步骤,但我无法找到项目属性来编辑
https://docs.google.com/document/pub? id = 19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

解决方案

如果您想在地图上显示用户的位置您需要的位置,位置API和Google Map API, b
$ b

这里是依赖链接放在您的应用程序级别中,

 依赖项{
compile'c​​om.google.android.gms:play-services-maps:10.0.1'
compile'c​​om.google.android .gms:play-services:10.0.1'
}

然后,您需要在ma上启动更新位置的位置服务p你还需要初始化你的地图,

在这里,我认为你初始化了你的地图显示,我只是展示了如何使用位置API,

  import android。*; 
导入android.manifest;
导入android.content.Context;
导入android.content.pm.PackageManager;
导入android.location.Location;
导入android.os.Build;
导入android.os.Bundle;
导入android.support.annotation.NonNull;
导入android.support.annotation.Nullable;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.content.ContextCompat;
导入android.support.v7.app.AppCompatActivity;
导入android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
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.model.LatLng;

public class MainLocationActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,LocationListener,GoogleApiClient.OnConnectionFailedListener {
private static final float DISTANCE_FOR_FISPLACEMENT = 2; //这里是计量器
私有上下文上下文中更新映射的距离;
私有GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
if(checkPermsion(context)){
setupLocationService(context);



public boolean checkPermsion(Context context){
int MyVersion = Build.VERSION.SDK_INT;
if(MyVersion> Build.VERSION_CODES.LOLLIPOP_MR1){
if(ContextCompat.checkSelfPermission(context,android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
return false;
} else if(ContextCompat.checkSelfPermission(context,android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
return false;
} else {
return true;
}
} else {
return true;



private void setupLocationService(Context context){
if(checkPlayServices()){
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
// .addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build() ;
createLocationRequest();

$ b protected void createLocationRequest(){
mLocationRequest = new LocationRequest()。create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
。 setFastestInterval(5 * 60 * 1000)
.setInterval(5 * 60 * 1000)
.setSmallestDisplacement(DISTANCE_FOR_FISPLACEMENT);
mGoogleApiClient.connect();

private boolean checkPlayServices(){
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(this);
if(result!= ConnectionResult.SUCCESS){
return false;
}
返回true;
}

@Override
public void onConnected(@Nullable Bundle bundle){
Log.i(TAG,Connected to onConnected);
startLocationUpdates();
}

@Override
public void onConnectionSuspended(int i){


private void startLocationUpdates(){
if(mGoogleApiClient.isConnected()){
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);



@Override
public void onLocationChanged(Location location){
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude (),location.getLongitude())));
//同样在这里,您可以在地图
}

@Override
中更改您的图钉public void onConnectionFailed(@NonNull ConnectionResult connectionResult){




I'm new to android and i'm trying to develop an android application which includes tracking the user location details and showing it on a google map and the location should be updated in the map whenever it is changed. I'm unable to understand how to get this through google maps v2. They mentioned to change dependencies but i'm unable to find where to do that can anyone explain the usage of this with a simple example i tried to implement the steps given in below link but i'm unable to find project properties to edit https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

解决方案

If you want to show location of user on map when the change their location you need to take, Location API and Google Map API,

here is dependency link put in your app level gradel,

dependencies {
    compile 'com.google.android.gms:play-services-maps:10.0.1'
    compile 'com.google.android.gms:play-services:10.0.1'
}

Then after you need to start location service for update location on map also you need to initialise your map,

Here i consider You initialised your map show I am just showing how to use location api,

import android.*;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
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.model.LatLng;

public class MainLocationActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, LocationListener, GoogleApiClient.OnConnectionFailedListener {
    private static final float DISTANCE_FOR_FISPLACEMENT = 2; //here is the distance for update map in meter
    private Context context;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
        if (checkPermsion(context)) {
            setupLocationService(context);
        }
    }

    public boolean checkPermsion(Context context) {
        int MyVersion = Build.VERSION.SDK_INT;
        if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return false;
            } else if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    private void setupLocationService(Context context) {
        if (checkPlayServices()) {
            mGoogleApiClient = new GoogleApiClient.Builder(context)
                    .addApi(LocationServices.API)
//                    .addApi(ActivityRecognition.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            createLocationRequest();
        }
    }
    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest().create()
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
                .setFastestInterval(5 * 60 * 1000)
                .setInterval(5 * 60 * 1000)
                .setSmallestDisplacement(DISTANCE_FOR_FISPLACEMENT);
        mGoogleApiClient.connect();
    }
    private boolean checkPlayServices() {
        GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
        int result = googleAPI.isGooglePlayServicesAvailable(this);
        if (result != ConnectionResult.SUCCESS) {
            return false;
        }
        return true;
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.i(TAG, "Connected to onConnected");
        startLocationUpdates();
    }

    @Override
    public void onConnectionSuspended(int i) {

    }
    private void startLocationUpdates() {
        if (mGoogleApiClient.isConnected()) {
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
        // Also here you can change your pin in map
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}

这篇关于使用谷歌地图v2更新位置查找器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆