Get address from location name, using Geocoder

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
    android.location.Geocoder is a class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary. The Geocoder class requires a backend service that is not included in the core android framework.

    The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.

    The method getFromLocationName() returns an array of Addresses that are known to describe the named location.

    example:

    [​IMG]

    (Please note that the retrieving of Address Line is in-correct in this code; refer to the next post for how to Get full address from result of getFromLocationName().)

    PHP:
    package com.AndroidgetFromLocationName;
     
    import java.io.IOException;
    import java.util.List;
     
    import android.app.Activity;
    import android.location.Address;
    import android.location.Geocoder;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class 
    AndroidgetFromLocationNameActivity extends Activity {
     
    EditText searchText;
    Button searchButton;
    TextView listResult;
     
    Geocoder myGeocoder;
    final static 
    int MAX_RESULT 5;
    //final static String DEFAULT_SEARCH = "1600 Amphitheatre Parkway Mountain View";
    //final static String DEFAULT_SEARCH = "Times Square";
    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);
     
    listResult = (TextView)findViewById(R.id.result);
     
     
    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();
     }
    }
     
    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{
     
    String stringResult "";
     for (
    int i =0result.size(); i++){
      
    stringResult += "i: " "\n"
        
    result.get(i).getAddressLine(i) + "\n"
        
    "lat: " result.get(i).getLatitude() + "\n"
        
    "lon: " result.get(i).getLongitude() + "\n";
     }
     
    listResult.setText(stringResult);
     
     
    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();
    }
    }
    }
    PHP:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
    <EditText
    android:id="@+id/searchtext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
    <Button
    android:id="@+id/searchbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Search"
    />
    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
    android:id="@+id/result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
    </ScrollView>
    </LinearLayout>
     
    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