English 中文(简体)
如何从安乐团所在地获得实际地址
原标题:How to get the actual address from a Location on Android
  • 时间:2011-10-23 02:23:34
  •  标签:
  • android

我想得到地点地址,我尝试:

package com.spot.prototype;

import android.os.Bundle;
import android.view.KeyEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.view.MotionEvent;
import android.widget.Toast;

import android.location.Geocoder;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

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

public class SpotActivity extends MapActivity {
    MapView mapView;
    MapController mc;
    GeoPoint p;

    private LocationManager lm;
    private LocationListener locationListener;

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );
        mapView = ( MapView ) findViewById( R.id.mapView );
        mapView.setBuiltInZoomControls( true );
        // mapView.setSatellite(true);
        // mapView.setStreetView(true);
        mc = mapView.getController();
        String coordinates[] = { "1.352566007", "103.78921587" };
        double lat = Double.parseDouble( coordinates[0] );
        double lng = Double.parseDouble( coordinates[1] );
        p = new GeoPoint( ( int ) ( lat * 1E6 ), ( int ) ( lng * 1E6 ) );
        mc.animateTo( p );
        mc.setZoom( 13 );

        lm = ( LocationManager ) getSystemService( Context.LOCATION_SERVICE );
        locationListener = new MyLocationListener();
        lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 200, 10, locationListener );
    }

    public boolean onKeyDown( int keyCode, KeyEvent event ) {
        MapController mc = mapView.getController();
        switch( keyCode ) {
        case KeyEvent.KEYCODE_3 :
            mc.zoomIn();
            break;
        case KeyEvent.KEYCODE_1 :
            mc.zoomOut();
            break;
        }
        return super.onKeyDown( keyCode, event );
    }



    private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged( Location loc ) {
            if ( loc != null ) {
                Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault() );
                try {
                    List<Address> addresses = geoCoder.getFromLocation( loc.getLatitude(), loc.getLongitude(), 1 );
                    System.out.println( loc.getLatitude() );
                    System.out.println( loc.getLongitude() );
                    String add = "";
                    if ( addresses.size() > 0 ) {
                        for( int i = 0; i < addresses.get( 0 ) .getMaxAddressLineIndex(); ++i ) {
                            add += addresses.get( 0 ).getAddressLine( i ) + "
";
                        }
                    }

                    Toast.makeText( getBaseContext(), add, Toast.LENGTH_SHORT ) .show();

                }  
                catch( IOException e ) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onProviderDisabled( String provider ) {
        }

        @Override
        public void onProviderEnabled( String provider ) {
        }

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

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

在向我的支持者发出信号时,我看到该地点实际上得到了更新。 但是,地址清单总是null。 任何人都能够让我了解这一点? 提前感谢。

最佳回答

http://developer.android.com/vis/android/position/Geocoder.html“rel=“nofollow”>getFromLocation(:

一份地址物体清单。 如果找不到对应物,或没有后备服务,则返回名单无效或空洞。

因此,如果你重新给予地址,实际上就与真实地址相对应,或者为了获得地址而需要运行的后备服务就没有运行。

Also from the geocoder documentation:

Geocoder类别需要一个不包括在核心和海底框架中的支持服务。 如果平台没有辅助服务,Geocoder查询方法将退回一份空名单。 采用“先决条件()”方法确定是否存在“地球科学”的实施。

你们是否试图用预先的方法来看待支持者是否正确?

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签