简体   繁体   中英

Get current location as TEXT with Google Maps API v2

I am currently retrieving the current location in my app but i also need the text of this location, or the city i am in at least, something like that.

Is there any known way i can do this?

This is what i have so far:

package ro.gebs.captoom.activities;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Window;

import com.example.captoom.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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 LocationActivity extends Activity {

    private GoogleMap map;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        getActionBar().hide();
        setContentView(R.layout.preview_location);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();

        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = service.getBestProvider(criteria, false);
        Location location = service.getLastKnownLocation(provider);
        LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());


        if (map != null) {
            Marker current_location_marker = map.addMarker(new MarkerOptions().position(userLocation)
                      .title("Current Location"));
        } else {

        }

        map.moveCamera(
                CameraUpdateFactory.newLatLngZoom(userLocation, 10));

        // Zoom in, animating the camera.
        map.animateCamera(
                CameraUpdateFactory.zoomTo(10), 2000, null);

    }

}

First you have to get your current latitude and longitude then you can use google api to get current address component. See my answer on this question to get address from lat long

Get the particular address using latitude and longitude

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM