Android示例中的反向地理编码器无法正常工作

时间:2014-03-03 03:49:33

标签: android geolocation location google-geocoder

我想创建一个获取位置地址的应用程序。我确实参考了一些教程,但响应是Geocoder不存在。我需要提供后端服务吗?如果有,怎么样?

这是代码:

package com.example.locationsample;

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

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Button getloc;
TextView lati;
TextView longi;
TextView address;

LocationManager location_manager;
String getLatitude;
String getLongitude;

double x;
double y;

Geocoder geocoder;
List<Address> addresses;
Location loc;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getloc = (Button) findViewById(R.id.getlocation);
    lati = (TextView) findViewById(R.id.latitude);
    longi = (TextView) findViewById(R.id.longitude);
    address = (TextView) findViewById(R.id.address);
    location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    getloc.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    LocationListener listner = new MyLocationListner();
    location_manager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 0, 0, listner);

    }
    });

    }
public class MyLocationListner implements LocationListener{




@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressWarnings("static-access")
@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    getLatitude = "" + arg0.getLatitude();
    getLongitude = "" + arg0.getLongitude();

    lati.setText(getLatitude);
    longi.setText(getLongitude);

    x = arg0.getLatitude();
    y = arg0.getLongitude();

    try {
    geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH);
    addresses = geocoder.getFromLocation(x, y, 1);
    StringBuilder str = new StringBuilder();
    if (geocoder.isPresent()) {
    Toast.makeText(getApplicationContext(),
    "geocoder present", Toast.LENGTH_SHORT).show();
    Address returnAddress = addresses.get(0);

    String localityString = returnAddress.getLocality();
    String city = returnAddress.getCountryName();
    String region_code = returnAddress.getCountryCode();
    String zipcode = returnAddress.getPostalCode();

    str.append(localityString + "");
    str.append(city + "" + region_code + "");
    str.append(zipcode + "");

    address.setText(str);
    Toast.makeText(getApplicationContext(), str,
    Toast.LENGTH_SHORT).show();

    } else {
        Log.e("y klaro","ndai ini");
    Toast.makeText(getApplicationContext(),
    "geocoder not present", Toast.LENGTH_SHORT).show();
    }

    // } else {
    // Toast.makeText(getApplicationContext(),
    // "address not available", Toast.LENGTH_SHORT).show();
    // }
    } catch (IOException e) {
    // TODO Auto-generated catch block

    Log.e("tag", e.getMessage());
    }

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

} }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.locationsample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.locationsample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

1 个答案:

答案 0 :(得分:1)

我对您现有的代码进行了一些改动,并且工作正常。

MainActivity.java

public class MainActivity extends Activity 
{
    Button getloc;
    TextView lati;
    TextView longi;
    TextView address;

    LocationManager location_manager;
    LocationListener listner;
    String getLatitude;
    String getLongitude;

    double x;
    double y;

    Geocoder geocoder;
    List<Address> addresses;
    Location loc;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getloc = (Button) findViewById(R.id.getlocation);
        lati = (TextView) findViewById(R.id.latitude);
        longi = (TextView) findViewById(R.id.longitude);
        address = (TextView) findViewById(R.id.address);
        location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listner = new MyLocationListner();

        getloc.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View arg0) 
            {
                location_manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10, listner);
                location_manager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
            }
        });
    }

    @Override
    public void onPause()
    {
        super.onPause();
        location_manager.removeUpdates(listner);
    }

    public class MyLocationListner implements LocationListener
    {
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @SuppressWarnings("static-access")
        @Override
        public void onLocationChanged(Location arg0) 
        {
            // TODO Auto-generated method stub
            getLatitude = "" + arg0.getLatitude();
            getLongitude = "" + arg0.getLongitude();

            lati.setText( getLatitude + "," + getLongitude );

            x = arg0.getLatitude();
            y = arg0.getLongitude();

            try 
            {
                geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH);
                addresses = geocoder.getFromLocation(x, y, 1);
                StringBuilder str = new StringBuilder();
                if (geocoder.isPresent())
                {
                    Toast.makeText(getApplicationContext(),
                            "geocoder present", Toast.LENGTH_SHORT).show();
                    Address returnAddress = addresses.get(0);

                    String localityString = returnAddress.getLocality();
                    String city = returnAddress.getCountryName();
                    String region_code = returnAddress.getCountryCode();
                    String zipcode = returnAddress.getPostalCode();

                    str.append(localityString + "");
                    str.append(city + "" + region_code + "");
                    str.append(zipcode + "");

                    longi.setText( str );

                    address.setText(str);
                    Toast.makeText(getApplicationContext(), str,Toast.LENGTH_SHORT).show();
                } 
                else 
                {
                    Toast.makeText(getApplicationContext(), "geocoder not present", Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e) {}
        }

        @Override
        public void onProviderDisabled(String arg0) {}

        @Override
        public void onProviderEnabled(String arg0) {}

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
    } 
}