Googleマップに住所の場所を表示したい。
Google Maps APIを使用して住所の緯度と経度を取得するにはどうすればよいですか?
Googleマップに住所の場所を表示したい。
Google Maps APIを使用して住所の緯度と経度を取得するにはどうすればよいですか?
public GeoPoint getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
strAddressアドレスを含む文字列です。address変数は、変換されたアドレスを保持します。
あなたがグーグルマップにあなたのアドレスを置きたいならば、それから以下を使う簡単な方法
Intent searchAddress = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
また
住所から緯度を取得する必要がある場合は、次のGooglePlaceApiを使用してください
次のようなHTTP呼び出しの応答でJSONObjectを返すメソッドを作成します
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
次に、そのJSONObjectを次のようにgetLatLong()メソッドに渡します。
public static boolean getLatLong(JSONObject jsonObject) {
try {
longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
return false;
}
return true;
}
これがあなたや他の人に役立つことを願っています..!! ありがとうございました..!!
これは、地図をクリックした場所の緯度と経度を見つける方法です。
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1)
{
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
それはうまくいきます。
場所の住所を取得するには、ジオコーダー クラスを使用できます。
上記のカンダ問題の答え:
「java.io.IOException service not available」をスローします。私はすでにそれらの許可を与えており、ライブラリを含めています...マップビューを取得できます...ジオコーダーでそのIOExceptionをスローします...
try の後に catch IOException を追加したところ、問題が解決しました
catch(IOException ioEx){
return null;
}
Geocoder coder = new Geocoder(this);
List<Address> addresses;
try {
addresses = coder.getFromLocationName(address, 5);
if (addresses == null) {
}
Address location = addresses.get(0);
double lat = location.getLatitude();
double lng = location.getLongitude();
Log.i("Lat",""+lat);
Log.i("Lng",""+lng);
LatLng latLng = new LatLng(lat,lng);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
googleMap.addMarker(markerOptions);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,12));
} catch (IOException e) {
e.printStackTrace();
}
public LatLang getLatLangFromAddress(String strAddress){
Geocoder coder = new Geocoder(this, Locale.getDefault());
List<Address> address;
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return new LatLang(-10000, -10000);
}
Address location = address.get(0);
return new LatLang(location.getLatitude(), location.getLongitude());
} catch (IOException e) {
return new LatLang(-10000, -10000);
}
}
LatLangこの場合は pojo クラスです。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />許可は必要ありません。