How to get latitude and longitude from address on Android?

给你一囗甜甜゛ 提交于 2019-12-23 03:29:09

问题


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

}

回答1:


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

or

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

use any one of the url just replace your desired address and have a network hit then it will return a list of related lat and long from google server.




回答2:


Try the geocoding service.Send an http request to the service then read the response and place a marker on the map.




回答3:


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


来源:https://stackoverflow.com/questions/10158373/how-to-get-latitude-and-longitude-from-address-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!