หน้าเว็บ

วันอังคารที่ 6 มีนาคม พ.ศ. 2555

GeoCoding and RequestLocation google map android

 

Geocoding.java
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

public class Geocoding {
    private Context context;
    public Geocoding(Context context){
        this.context = context;
    }
    
    public String requestGPS2Address(RequestLocation requstl) {
        //input is RequestLocation, output is address string
        String response = "";
        if (requstl.getLocation() != null) {
            List<Address> addresses;
            Geocoder goc = new Geocoder(context, Locale.ENGLISH);
            response = "Location : \n";
            response += "Latitude " + requstl.getLatitude() + "\n";
            response += "Longitude " + requstl.getLongitude() + "\n\n";
            try {
                addresses = goc.getFromLocation(
                    requstl.getLatitude(),
                    requstl.getLongitude(), 
                    1
                );
                if (addresses != null) {
                    Address currentAddr = addresses.get(0);
                    for (int i = 0;  i < currentAddr.getMaxAddressLineIndex(); i++) {
                        response += currentAddr.getAddressLine(i) + "\n";
                    }
                }
            } catch (IOException e) {
                response = e.getMessage() + " (please check your internet provider)";
            }
        } else {
            response = "No "+requstl.getProvider()+" Signal";
        }
        return response;
    }
}

RequestLocation.java
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class RequestLocation implements LocationListener {
    private Location location;
    private String provider;
    private int TIME_FOR_UPDATE_NEW_LOCATION; 
    private int DISTANCE_FOR_UPDATE_NEW_LOCATION;
    private LocationManager locationmanager;
    
    public RequestLocation(LocationManager lm){
        TIME_FOR_UPDATE_NEW_LOCATION = 1000; //millisecond
        DISTANCE_FOR_UPDATE_NEW_LOCATION = 1; //meter
        locationmanager = lm;
        setAutoProvider();
        setRequestLocationUpdate();
    }
    
    /* ==================== User Create Method ========================= */
    public void setRequestLocationUpdate(){
        locationmanager.requestLocationUpdates(
            provider,
            TIME_FOR_UPDATE_NEW_LOCATION,
            DISTANCE_FOR_UPDATE_NEW_LOCATION, 
            this // this class
        );
    }
     
    public void setGPSProvider(){
        provider = LocationManager.GPS_PROVIDER;
        location = locationmanager.getLastKnownLocation(provider);
    }
    
    public void setNetworkProvider(){
        provider = LocationManager.NETWORK_PROVIDER;
        location = locationmanager.getLastKnownLocation(provider);
    }
    
    public void setAutoProvider(){
        if(locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER) == true){
            provider = LocationManager.GPS_PROVIDER;
        }else {
            provider = LocationManager.NETWORK_PROVIDER;
        }
        location = locationmanager.getLastKnownLocation(provider);
    }
    
    public String getProvider(){
        return provider;
    }
    
    public boolean isGPSTurnOn(){
        return locationmanager.isProviderEnabled(provider);
    }
    
    public void setTimeForUpdateLocation(int time_is_millisecond){
        TIME_FOR_UPDATE_NEW_LOCATION = time_is_millisecond;
    }
    
    public void setDistanceForUpdateLoation(int distance_is_meter){
        DISTANCE_FOR_UPDATE_NEW_LOCATION = distance_is_meter;
    }
    
    public void setDistanceTimeForUpdateLocation(int distance_is_meter, int time_is_millisecond){
        DISTANCE_FOR_UPDATE_NEW_LOCATION = distance_is_meter;
        TIME_FOR_UPDATE_NEW_LOCATION = time_is_millisecond;
    }
    
    public Location getLocation(){
        return location;
    }
    
    public double getLatitude(){
        return location.getLatitude();
    }
    
    public double getLongitude(){
        return location.getLongitude();
    }
    
    /* ======================= Override Method ===================== */
    @Override
    public void onLocationChanged(Location loc) {
        if(loc!=null){
            location = loc;
        }
    }

    @Override
    public void onProviderDisabled(String provider) { /* Not use */ }

    @Override
    public void onProviderEnabled(String provider) { /* Not use */ }

    @Override
    public void onStatusChanged(
        String provider, 
        int status, 
        Bundle extras
    ) { /* Not use */ }
}

WhereisActivity.java
import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class WhereisActivity extends Activity {
    private RequestLocation requestl;
    private Geocoding geocoding;
    
    private TextView result_area;
    private ImageButton request_button;
    
    @Override
    public void onCreate(Bundle sevedInstanceState) {
        super.onCreate(sevedInstanceState);
        setContentView(R.layout.whereis_activity);

        result_area = (TextView) findViewById(R.id.textviewAreaResultGPS2Address);
        request_button = (ImageButton) findViewById(R.id.imageButtonRequestGPS2Address);

        requestl = new RequestLocation((LocationManager) getSystemService(Context.LOCATION_SERVICE));
        geocoding = new Geocoding(WhereisActivity.this);
        
        request_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(requestl.isGPSTurnOn() == true) {
                    updateResult(geocoding.requestGPS2Address(requestl));
                } else {
                    showOutputScreen("Please eneble " + requestl.getProvider() + " provider");
               }
           }
        });
    }

    public void updateResult(String r) {
        result_area.setText(r);
    }
    
    private void showOutputScreen(String s) {
        Toast.makeText(getBaseContext(), s, Toast.LENGTH_SHORT).show();
    }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น