【问题标题】:find the location of any place through text in android在android中通过文本找到任何地方的位置
【发布时间】:2013-06-23 08:21:31
【问题描述】:

我是 android 开发新手。我想在 我的手机的哪个位置应该通过文本显示。 (不是通过纬度和经度或通过地图视图)。 我的意思是它应该用文字显示位置...... 喜欢...[您的位置:印度新德里环路]

在android中可以吗.. 当我尝试这样做时,我只能获得纬度和经度或查看地图,但我无法用文字获得位置 如果是,请告诉我代码。 这是银色的朋友。请告诉我..

谢谢

【问题讨论】:

    标签: android geolocation android-location


    【解决方案1】:

    您需要使用GeoCoder 来检索地址信息。

     Geocoder gcd = new Geocoder(activity.getActivity().getBaseContext(),   Locale.getDefault());  
     List<Address> addresses;              
     addresses = gcd.getFromLocation(latitude,longitude, 1);  
    

    【讨论】:

      【解决方案2】:
      public String ConvertPointToLocation(GeoPoint point) {   
                  String address = "";
                  Geocoder geoCoder = new Geocoder(
                          getBaseContext(), Locale.getDefault());
                  try {
                      List<Address> addresses = geoCoder.getFromLocation(
                          point.getLatitudeE6()  / 1E6, 
                          point.getLongitudeE6() / 1E6, 1);
      
                      if (addresses.size() > 0) {
                          for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                              address += addresses.get(0).getAddressLine(index) + " ";
                      }
                  }
                  catch (IOException e) {                
                      e.printStackTrace();
                  }   
      
                  return address;
              } 
      

      完整代码

      public class GPSLocatorActivity extends MapActivity {
          private MapView mapView;
          private MapController mapController;
      
          private LocationManager locationManager;
          private LocationListener locationListener;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
      
              locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
      
              locationListener = new GPSLocationListener();
      
              locationManager.requestLocationUpdates(
                  LocationManager.GPS_PROVIDER, 
                  0, 
                  0, 
                  locationListener);
      
              mapView = (MapView) findViewById(R.id.mapView);
      
              // enable Street view by default
              mapView.setStreetView(true);
      
              // enable to show Satellite view
              // mapView.setSatellite(true);
      
              // enable to show Traffic on map
              // mapView.setTraffic(true);
              mapView.setBuiltInZoomControls(true);
      
              mapController = mapView.getController();
              mapController.setZoom(16); 
          }
      
          @Override
          protected boolean isRouteDisplayed() {
              return false;
          }
      
          private class GPSLocationListener implements LocationListener 
          {
              @Override
              public void onLocationChanged(Location location) {
                  if (location != null) {
                      GeoPoint point = new GeoPoint(
                              (int) (location.getLatitude() * 1E6), 
                              (int) (location.getLongitude() * 1E6));
      
                      /* Toast.makeText(getBaseContext(), 
                              "Latitude: " + location.getLatitude() + 
                              " Longitude: " + location.getLongitude(), 
                              Toast.LENGTH_SHORT).show();*/
      
                      mapController.animateTo(point);
                      mapController.setZoom(16);
      
                      // add marker
                      MapOverlay mapOverlay = new MapOverlay();
                      mapOverlay.setPointToDraw(point);
                      List<Overlay> listOfOverlays = mapView.getOverlays();
                      listOfOverlays.clear();
                      listOfOverlays.add(mapOverlay);
                      String address = ConvertPointToLocation(point);
                      Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
                      mapView.invalidate();
                  }
              }
      
              public String ConvertPointToLocation(GeoPoint point) {   
                  String address = "";
                  Geocoder geoCoder = new Geocoder(
                          getBaseContext(), Locale.getDefault());
                  try {
                      List<Address> addresses = geoCoder.getFromLocation(
                          point.getLatitudeE6()  / 1E6, 
                          point.getLongitudeE6() / 1E6, 1);
      
                      if (addresses.size() > 0) {
                          for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                              address += addresses.get(0).getAddressLine(index) + " ";
                      }
                  }
                  catch (IOException e) {                
                      e.printStackTrace();
                  }   
      
                  return address;
              } 
      
              @Override
              public void onProviderDisabled(String provider) {
              }
      
              @Override
              public void onProviderEnabled(String provider) {
              }
      
              @Override
              public void onStatusChanged(String provider, int status, Bundle extras) {
              }
          }
      
          class MapOverlay extends Overlay
          {
              private GeoPoint pointToDraw;
      
              public void setPointToDraw(GeoPoint point) {
                  pointToDraw = point;
              }
      
              public GeoPoint getPointToDraw() {
                  return pointToDraw;
              }
      
              @Override
              public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
                  super.draw(canvas, mapView, shadow);                   
      
                  // convert point to pixels
                  Point screenPts = new Point();
                  mapView.getProjection().toPixels(pointToDraw, screenPts);
      
                  // add marker
                  Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
                  canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image        
                  return true;
              }
          } 
      }
      

      【讨论】:

      • 你必须有源代码。请发送给我。在我的邮件中--kuldeep10m@gmail..com。我非常需要它
      猜你喜欢
      • 2013-06-27
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多