get location from Google Chinese - Android Map

Android examples for Map:Google Map

Description

get location from Google Chinese

Demo Code


//package com.java2s;

import java.io.IOException;

import java.util.List;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class Main {
    public static String cityName;
    private static Geocoder geocoder;

    private final static LocationListener locationListener = new LocationListener() {
        String tempCityName;/* w ww. ja v  a2s . c  o  m*/

        public void onLocationChanged(Location location) {

            tempCityName = updateWithNewLocation(location);
            if ((tempCityName != null) && (tempCityName.length() != 0)) {

                cityName = tempCityName;
            }
        }

        public void onProviderDisabled(String provider) {
            tempCityName = updateWithNewLocation(null);
            if ((tempCityName != null) && (tempCityName.length() != 0)) {
                cityName = tempCityName;
            }
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }
    };

    public static String getCNBylocation(Context context) {

        geocoder = new Geocoder(context);
        //Location?  
        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        //?LocationManager  
        locationManager = (LocationManager) context
                .getSystemService(serviceName);
        //provider?  
        String provider = LocationManager.NETWORK_PROVIDER;

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE); //?  
        criteria.setAltitudeRequired(false); //?  
        criteria.setBearingRequired(false); //  
        criteria.setCostAllowed(false); //?  
        criteria.setPowerRequirement(Criteria.POWER_LOW); //?  

        //?Location  
        Location location = locationManager.getLastKnownLocation(provider);

        String queryed_name = updateWithNewLocation(location);
        if ((queryed_name != null) && (0 != queryed_name.length())) {

            cityName = queryed_name;
        }

        /* 
         *  
         * 30 
         */
        locationManager.requestLocationUpdates(provider, 30000, 50,
                locationListener);
        //?widget?  
        locationManager.removeUpdates(locationListener);

        return cityName;
    }

    private static String updateWithNewLocation(Location location) {
        String mcityName = "";
        double lat = 0;
        double lng = 0;
        List<Address> addList = null;
        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();
        } else {

            System.out.println("");
        }

        try {

            addList = geocoder.getFromLocation(lat, lng, 1); //?  

        } catch (IOException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }
        if (addList != null && addList.size() > 0) {
            for (int i = 0; i < addList.size(); i++) {
                Address add = addList.get(i);
                mcityName += add.getLocality();
            }
        }
        if (mcityName.length() != 0) {

            return mcityName.substring(0, (mcityName.length() - 1));
        } else {
            return mcityName;
        }
    }
}

Related Tutorials