get latitude and longitude from android.location.Address

Thảo luận trong 'Điện thoại, viễn thông' bắt đầu bởi bka15, 30/9/12.

  1. bka15 New Member

    bka15

    Tham gia ngày:
    18/7/12
    Bài viết:
    128
    Đã được thích:
    105
    Điểm thành tích:
    0
    The result returned from Geocoder.getFromLocationName() is a List of android.location.Address object. To retrieve the latitude and longitude of the selected item's Address, getLatitude() and getLongitude() methods can be used.

    [​IMG]


    Further work on last post, Display address from Geocoder.getFromLocationName() in a ListView. Implement listviewResultOnItemClickListener of OnItemClickListener to handle the item click event, and display the location (latitude and longitude) in Toast.



    PHP:
    package com.AndroidgetFromLocationName;
     
    import java.io.IOException;
    import java.util.List;
     
    import android.app.Activity;
    import android.content.Context;
    import android.location.Address;
    import android.location.Geocoder;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class AndroidgetFromLocationNameActivity extends Activity {
     
     EditText searchText;
     Button searchButton;
     ListView listviewResult;
      
     Geocoder myGeocoder;
     final static int MAX_RESULT = 10;
     final static String DEFAULT_SEARCH = "谷歌";
      
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          searchText = (EditText)findViewById(R.id.searchtext);
          searchText.setText(DEFAULT_SEARCH);
       searchButton = (Button)findViewById(R.id.searchbutton);
       listviewResult = (ListView)findViewById(R.id.listviewresult);
        
       searchButton.setOnClickListener(searchButtonOnClickListener);
        
       myGeocoder = new Geocoder(this);
        
       //for API Level 9 or higher
       if (!Geocoder.isPresent()){
        Toast.makeText(AndroidgetFromLocationNameActivity.this,
         "Sorry! Geocoder service not Present.",
         Toast.LENGTH_LONG).show();
       }
        
       listviewResult.setOnItemClickListener(listviewResultOnItemClickListener);
      }
     
      OnItemClickListener listviewResultOnItemClickListener
      = new OnItemClickListener(){
     
      @Override
      public void onItemClick(AdapterView<?parentView viewint position,
        
    long id) {
       
    // TODO Auto-generated method stub
        
       
    double lat = ((Address)parent.getItemAtPosition(position)).getLatitude();
       
    double lon = ((Address)parent.getItemAtPosition(position)).getLongitude();
       
    String loc "lat: " lat "\n" "lon: " lon;
       
    Toast.makeText(AndroidgetFromLocationNameActivity.this,
         
    loc,
         
    Toast.LENGTH_LONG).show();
      }
       
     };
     
      
    Button.OnClickListener searchButtonOnClickListener
      
    = new Button.OnClickListener(){
     
      @
    Override
      
    public void onClick(View arg0) {
       
    // TODO Auto-generated method stub
       
    String searchString searchText.getText().toString();
       
    searchFromLocationName(searchString);
      }};
       
     private 
    void searchFromLocationName(String name){
      try {
       List<
    Addressresult
       
    myGeocoder.getFromLocationName(nameMAX_RESULT);
        
       if ((
    result == null)||(result.isEmpty())){
        
    Toast.makeText(AndroidgetFromLocationNameActivity.this,
          
    "No matches were found or there is no backend service!",
          
    Toast.LENGTH_LONG).show();
       }else{
     
        
    MyArrayAdapter adapter = new MyArrayAdapter(this,
              
    android.R.layout.simple_list_item_1result);
        
    listviewResult.setAdapter(adapter);
         
        
    Toast.makeText(AndroidgetFromLocationNameActivity.this,
          
    "Finished!",
          
    Toast.LENGTH_LONG).show();
       }
     
        
      } catch (
    IOException e) {
       
    // TODO Auto-generated catch block
       
    e.printStackTrace();
       
    Toast.makeText(AndroidgetFromLocationNameActivity.this,
         
    "The network is unavailable or any other I/O problem occurs!",
         
    Toast.LENGTH_LONG).show();
      }
     }
      
     public class 
    MyArrayAdapter extends ArrayAdapter<Address> {
      
    Context mycontext;
      public 
    MyArrayAdapter(Context contextint textViewResourceId,
        List<
    Addressobjects) {
       
    super(contexttextViewResourceIdobjects);
       
    // TODO Auto-generated constructor stub
       
    mycontext context;
      }
     
      @
    Override
      
    public View getView(int positionView convertViewViewGroup parent) {
       
    // TODO Auto-generated method stub
        
       
    int maxAddressLineIndex getItem(position).getMaxAddressLineIndex();
       
    String addressLine "";
        
       for (
    int j 0<= maxAddressLineIndexj++){
        
    addressLine += getItem(position).getAddressLine(j) + ",";
       }
        
       
    TextView rowAddress = new TextView(mycontext);
       
    rowAddress.setText(addressLine);
        
       return 
    rowAddress;
     
      }
     
     }
      
    }
    next:
    - Start Google Maps using intent with specified location
     
    2 people like this.
    Đang tải...
  2. nguyenson.glhd New Member

    nguyenson.glhd

    Tham gia ngày:
    8/4/13
    Bài viết:
    31
    Đã được thích:
    0
    Điểm thành tích:
    0
    Giới tính:
    Nam
    Bài viết hay.
     

Chia sẻ trang này