如何从地址的经度和纬度在Android? [英] How to get latitude and longitude from address on Android?

查看:160
本文介绍了如何从地址的经度和纬度在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的初学者,我正在努力创建谷歌地图,它能够从标地址的具体位置。

现在,我下面code是能够标记帝国大厦,但没有其他..:其中;
我想知道如何从街道地址或完整地址的经纬度,并标记在地图上。

我修改清单文件支持谷歌地图视图,如互联网和ACCESS_COARSE_LOCATION

感谢您。

 包cs2340.kiwi.HelloGoogleMaps;进口java.io.IOException异常;
进口的java.util.List;
进口java.util.Locale中;进口android.graphics.drawable.Drawable;
进口android.location.Address;
进口android.location.Geo codeR;
进口android.os.Bundle;进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
进口com.google.android.maps.Overlay;
进口com.google.android.maps.OverlayItem;公共类HelloGoogleMapsActivity扩展MapActivity {
    / **当第一次创建活动调用。 * /
    @覆盖
    保护布尔isRouteDisplayed(){
        返回false;
    }
    公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    MapView类MapView类=(图形页面)findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(真);    清单<&叠加GT; mapOverlays =调用MapView.getOverlays();
    可绘制可绘制= this.getResources()getDrawable(R.drawable.androidmarker)。
    HelloItemizedOverlay itemizedoverlay =新HelloItemizedOverlay(绘制,这一点);    地理codeR codeR =新的地缘codeR(HelloGoogleMapsActivity.this,Locale.getDefault());    清单<地址>地址;
    字符串strAddress =帝国大厦;
    GeoPoint的P1 =新的GeoPoint(0,0);
    双纬度;
    双经度;
    MapController MC = mapView.getController();    尝试{
        地址= coder.getFromLocationName(strAddress,5);
        地址位置= address.get(0);
        纬度= location.getLatitude()* 1E6;
        经度= location.getLongitude()* 1E6;        P1 =新的GeoPoint(latitude.intValue()
                           longitude.intValue());        mc.animateTo(P1);
        mapView.invalidate();
    }
    赶上(IOException异常五){}
    OverlayItem overlayitem2 =新OverlayItem(P1,你好,不工作);    itemizedoverlay.addOverlay(overlayitem2);
    mapOverlays.add(itemizedoverlay);}
}

下面是我的另一个类

 包cs2340.kiwi.HelloGoogleMaps;进口的java.util.ArrayList;进口android.app.AlertDialog;
进口android.content.Context;
进口android.graphics.drawable.Drawable;进口com.google.android.maps.ItemizedOverlay;
进口com.google.android.maps.OverlayItem;公共类HelloItemizedOverlay扩展ItemizedOverlay {私人的ArrayList< OverlayItem> mOverlays =新的ArrayList< OverlayItem>();
私人语境mContext;公共HelloItemizedOverlay(可绘制defaultMarker,上下文的背景下){
      超(boundCenterBottom(defaultMarker));
      mContext =背景;
    }公共无效addOverlay(OverlayItem叠加){
    mOverlays.add(覆盖);
    填充();
}保护OverlayItem createItem中(int i)以{
      返回mOverlays.get(ⅰ);
}公众诠释大小(){
      返回mOverlays.size();
}保护布尔中的onTap(INT指数){
      OverlayItem项目= mOverlays.get(指数);
      AlertDialog.Builder对话框=新AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      返回true;
}}


解决方案

要获得千万以下的街道地址或完整地址纬度和经度:

  INT的maxResults = 5;
字符串的地址=举例道15;地理codeR地理=新的地缘codeR(背景下,Locale.getDefault());
清单<地址>地址= geo.getFromLocationName(地址的maxResults);对于(地址A:不会忽略)
   map.addMarker(。新的MarkerOptions()位置(经纬度新(a.getLatitude(),a.getLongitude())).title伪(你好)是.snippet(关于我的描述!));

I am beginner in Android and I am working to create the Google Map which is able to mark the specific location from address.

For now, my following code is able to mark "empire state building" but nothing others..:< I want to know how to get latitude and longitude from street address or full address, and mark it on the map.

I modified the manifest file for supporting Google Map view like INTERNET and ACCESS_COARSE_LOCATION

Thank you.

package cs2340.kiwi.HelloGoogleMaps;

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

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;

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.Overlay;
import com.google.android.maps.OverlayItem;

public class HelloGoogleMapsActivity extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);

    Geocoder coder = new Geocoder(HelloGoogleMapsActivity.this, Locale.getDefault());

    List<Address> address;
    String strAddress = "empire state building";
    GeoPoint p1 = new GeoPoint(0,0);
    Double latitude;
    Double longitude;
    MapController mc = mapView.getController();

    try {
        address = coder.getFromLocationName(strAddress,5);
        Address location = address.get(0);
        latitude = location.getLatitude() * 1E6;        
        longitude = location.getLongitude() * 1E6;

        p1 = new GeoPoint( latitude.intValue(),
                           longitude.intValue());

        mc.animateTo(p1);    
        mapView.invalidate();
    }
    catch (IOException e){}
    OverlayItem overlayitem2 = new OverlayItem(p1, "Hello", "not working");

    itemizedoverlay.addOverlay(overlayitem2);
    mapOverlays.add(itemizedoverlay);

}
}

Here is my another class

package cs2340.kiwi.HelloGoogleMaps;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class HelloItemizedOverlay extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
      super(boundCenterBottom(defaultMarker));
      mContext = context;
    }

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

protected OverlayItem createItem(int i) {
      return mOverlays.get(i);
}

public int size() {
      return mOverlays.size();
}

protected boolean onTap(int index) {
      OverlayItem item = mOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
}

}

解决方案

To get Latitude and Longitude from street address or full address do following:

int maxResults = 5;
String address = "Example Road 15";

Geocoder geo = new Geocoder( context, Locale.getDefault() );
List<Address> addresses = geo.getFromLocationName( address, maxResults );

for ( Address a : adresses ) 
   map.addMarker( new MarkerOptions().position( new LatLng( a.getLatitude(), a.getLongitude() ) ).title( "Hello" ).snippet( "Description about me!" ) );

这篇关于如何从地址的经度和纬度在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆