本文介绍了使用Android Studio上的Maps API获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问,如何为Android创建脚本,以使用适用于Android的Google Maps api v2获取当前位置.现在,我的scrpt会选择指定坐标的位置.我将其发布到我的Java文件中:

I ask, how to create a script, for android, to get a current location with google maps api v2 for android.Now my scrpt take the location specifying the cordinates.I post it my java file:

private GoogleMap mMap;

static final LatLng TutorialsPoint = new LatLng(46.493168,11.3306379);
private GoogleMap googleMap;

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

    try {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().
                    findFragmentById(R.id.map)).getMap();
        }
        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        Marker TP = googleMap.addMarker(new MarkerOptions().
                position(TutorialsPoint).title("TutorialsPoint"));
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

推荐答案

在您的onCreate()方法中,键入以下内容:

In your onCreate() method, type this:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapfragment);
    mapFragment.getMapAsync(this);

使您的Activity实现LocationListeneronMapReadyCallbackGoogleApiClient.ConnectionCallbacks接口.在该接口的方法中:

Make your Activity implement LocationListener, onMapReadyCallback and GoogleApiClient.ConnectionCallbacks interfaces. In the methods of that interfaces:

onMapReady (from onMapReadyCallbacks):

    public void onMapReady(GoogleMap map) {
        //mapa is your GoogleMap variable
        mapa=map;

    }

这样做,请确保您的地图已准备就绪.

Doing this, you make sure your map is ready for working.

在onConnected方法(通过GoogleApiClient.ConnectionCallbacks界面)中,您可以找到位置:

In onConnected method (from GoogleApiClient.ConnectionCallbacks interface) you get your location:

@Override
public void onConnected(Bundle bundle) {

    Location lkn=LocationServices.FusedLocationApi.getLastLocation(
            gApiClient);
    if(myLocation==null){
        myLocation=new Location("");
    }
    if(lkn!=null){

        myLocation.setLatitude(lkn.getLatitude());
        myLocation.setLongitude(lkn.getLongitude());
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(
            gApiClient, mLocationRequest, this);
}

最后,在onLocationChanged中(从LocationListener接口):

And finally, in onLocationChanged (from LocationListener interface):

@Override
public void onLocationChanged(Location location) {
    //myLocation is a class variable, type Location
    myLocation.setLatitude(location.getLatitude());
    myLocation.setLongitude(location.getLongitude());

}

因此,每当手机有新位置时,您都会通过该方法收到它.

So, every time the phone have a new location you will receive it in that method.

您也将需要实现这两种方法:

You will need to implement this two methods too:

//call this method in your onCreate()
protected synchronized void buildGoogleApiClient() {
    //gApiClient is a class variable, type GoogleApiClient
    gApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    Log.i("Map", "Conection failed");
                }
            })
            .addApi(LocationServices.API)
            .build();
    gApiClient.connect();
    createLocationRequest();
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

这篇关于使用Android Studio上的Maps API获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:30