簡體   English   中英

如何從Android上的地址獲取經度和緯度?

[英]How to get latitude and longitude from address on Android?

我是Android的初學者,我正在努力創建一個能夠從地址標記特定位置的Google Map。

現在,我的下面的代碼能夠標記“帝國大廈”,但沒有其他任何東西......:<我想知道如何從街道地址或完整地址獲取緯度和經度,並在地圖上標記它。

我修改了清單文件以支持Google地圖視圖,例如INTERNET和ACCESS_COARSE_LOCATION

謝謝。

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);

}
}

這是我的另一堂課

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;
}

}

嘗試地理編碼服務。服務發送http請求,然后讀取響應並在地圖上放置標記。

http://maps.googleapis.com/maps/api/geocode/json?address=hyderabad&sensor=true_or_false

要么

http://maps.googleapis.com/maps/api/geocode/xml?address=hyderabad&sensor=true_or_false

使用任何一個網址只需替換您想要的地址並進行網絡點擊,然后它將從谷歌服務器返回相關的緯度和長度列表。

要從街道地址或完整地址獲取緯度和經度,請執行以下操作:

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!" ) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM