我写一个应用程序,需要获取当前地图上的位置。我的地图文件工作正常,通过它的自我,但我需要从另一个活动$ C获得地址(请参阅 addressString 要低于BUTTOM) $ C>。我试过的getAddress / setAddress (设置器/吸气)。他们不工作。他们总是返回无地址(默认值)。
下面是我的code ...
我怎样才能使这是一个独立的Java类???或者从另一个活动获得的地址?
多谢了。
这code工作本身。
垫
进口android.content.Context; 进口android.location.Address; 进口android.location.Criteria; 进口android.location.Geo codeR; 进口android.location.Location; 进口android.location.LocationListener; 进口android.location.LocationManager; 进口android.os.Bundle; 进口android.widget.TextView; 进口java.io.IOException异常; 进口的java.util.List; 进口java.util.Locale中; 公共类GetMapAddress扩展MapActivity { MapController mapController; MyPositionOverlay positionOverlay; MapController MC; 的GeoPoint磷; 字符串addressString =没有地址找到; @覆盖 公共无效的onCreate(包冰柱){ super.onCreate(冰柱); 的setContentView(R.layout.map); 图形页面myMapView =(图形页面)findViewById(R.id.myMapView); mapController = myMapView.getController(); //配置图的显示选项 myMapView.setSatellite(真正的); myMapView.setStreetView(真正的); // 放大 mapController.setZoom(17); myMapView.setBuiltInZoomControls(真正的); //添加MyPositionOverlay positionOverlay =新MyPositionOverlay(); 名单<覆盖>叠加= myMapView.getOverlays(); overlays.add(positionOverlay); LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); 标准标准=新标准(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(假); criteria.setBearingRequired(假); criteria.setCostAllowed(真正的); criteria.setPowerRequirement(Criteria.POWER_LOW); 字符串商= locationManager.getBestProvider(标准,真正的); 位置位置= locationManager.getLastKnownLocation(供应商); updateWithNewLocation(位置); locationManager.requestLocationUpdates(供应商,2000,10,LocationListener的); MC = myMapView.getController(); 字符串的坐标[] = { 1.352566007,103.78921587 }; 双纬度= Double.parseDouble(坐标[0]); 双LNG = Double.parseDouble(坐标[1]); P =新的GeoPoint((INT)(LAT * 1E6),(INT)(LNG * 1E6)); mc.animateTo(对); mc.setZoom(17); myMapView.invalidate(); } 私人最终LocationListener的LocationListener的=新LocationListener的(){ 公共无效onLocationChanged(位置定位){ updateWithNewLocation(位置); } 公共无效onProviderDisabled(字符串提供商){ updateWithNewLocation(空); } 公共无效onProviderEnabled(字符串提供商){ } 公共无效onStatusChanged(字符串商,INT地位,捆绑演员){ } }; / **一个新的位置*更新地图/ 私人无效updateWithNewLocation(位置定位){ TextView的myLocationText =(TextView中)findViewById(R.id.myLocationText); 字符串latLongString; 如果(位置!= NULL){ //更新我的位置标记 positionOverlay.setLocation(位置); //更新地图上的位置。 双geoLat = location.getLatitude()* 1E6; 双geoLng = location.getLongitude()* 1E6; 的GeoPoint点=新的GeoPoint(geoLat.intValue(),geoLng.intValue()); mapController.animateTo(点); 双纬度= location.getLatitude(); 双LNG = location.getLongitude(); latLongString =纬度:+纬度+\ nLong:+ LNG; 地理codeR GC =新的地缘codeR(这一点,Locale.getDefault()); 尝试 { 名单<地址>地址= gc.getFromLocation(纬度,经度,1); StringBuilder的SB =新的StringBuilder(); 如果(addresses.size()大于0){ 地址地址= addresses.get(0); 的for(int i = 0; I< address.getMaxAddressLineIndex();我++) 。sb.append(address.getAddressLine(i))的追加(\ N); sb.append(address.getCountryName()); } addressString = sb.toString(); }赶上(IOException异常E){ } } 其他 { //在这里放置的CellID latLongString =找不到位置; } //此注释掉线将包括latitute和longtitute // myLocationText.setText(你的手机是目前在.. \ N+ latLongString +\ N+ // addressString); myLocationText.setText(你的手机是目前在.. \ N+ addressString); setAddress(addressString); ; } 公共无效setAddress(字符串添加){ this.addressString =增加; } 公共字符串的getAddress(){ 返回addressString; } @覆盖 保护的布尔isRouteDisplayed(){ 返回false; } }解决方案
我使用了下列code。在我的应用程序,其工作完全正常,我。尝试这个。希望这会帮助你。
// //写的地点名称。 // 尝试 { 地理codeR地理=新的地缘codeR(youractivityclassname.this.getApplicationContext(),Locale.getDefault()); 名单<地址>地址= geo.getFromLocation(纬度,经度,1); 如果(addresses.isEmpty()){ yourtextfieldname.setText(等待位置); } 其他 { 如果(addresses.size()大于0){ yourtextfieldname.setText(addresses.get(0).getFeatureName()+,+ addresses.get(0).getLocality()+,+ addresses.get(0).getAdminArea()+,+地址获得(0).getCountryName()); //Toast.makeText(getApplicationContext(),地址: - + addresses.get(0).getFeatureName()+ addresses.get(0).getAdminArea()+ addresses.get(0).getLocality(),面包.LENGTH_LONG).show(); } } } 赶上(例外五){ e.printStackTrace(); // getFromLocation()可能有时会失败 }I am writing an app that requires to get the current map location. My Map file works fine by it self, but I need to get the address (see addressString below at the buttom) from another Activity. I tried getAddress/setAddress (setters/getters). They do not work. They always return 'no address' (the default).
Here is my code...
How can I make this a standalone Java class??? Or get the address from another activity?
Thanks a lot.
This code works by itself.
Mat
import android.content.Context; import android.location.Address; import android.location.Criteria; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; import java.io.IOException; import java.util.List; import java.util.Locale; public class GetMapAddress extends MapActivity { MapController mapController; MyPositionOverlay positionOverlay; MapController mc; GeoPoint p; String addressString = "No address found"; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.map); MapView myMapView = (MapView) findViewById(R.id.myMapView); mapController = myMapView.getController(); // Configure the map display options myMapView.setSatellite(true); myMapView.setStreetView(true); // Zoom in mapController.setZoom(17); myMapView.setBuiltInZoomControls(true); // Add the MyPositionOverlay positionOverlay = new MyPositionOverlay(); List<Overlay> overlays = myMapView.getOverlays(); overlays.add(positionOverlay); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); mc = myMapView.getController(); String coordinates[] = { "1.352566007", "103.78921587" }; double lat = Double.parseDouble(coordinates[0]); double lng = Double.parseDouble(coordinates[1]); p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); mc.animateTo(p); mc.setZoom(17); myMapView.invalidate(); } private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location); } public void onProviderDisabled(String provider) { updateWithNewLocation(null); } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } }; /** Update the map with a new location */ private void updateWithNewLocation(Location location) { TextView myLocationText = (TextView) findViewById(R.id.myLocationText); String latLongString; if (location != null) { // Update my location marker positionOverlay.setLocation(location); // Update the map location. Double geoLat = location.getLatitude() * 1E6; Double geoLng = location.getLongitude() * 1E6; GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue()); mapController.animateTo(point); double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "Lat:" + lat + "\nLong:" + lng; Geocoder gc = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = gc.getFromLocation(lat, lng, 1); StringBuilder sb = new StringBuilder(); if (addresses.size() > 0) { Address address = addresses.get(0); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) sb.append(address.getAddressLine(i)).append("\n"); sb.append(address.getCountryName()); } addressString = sb.toString(); } catch (IOException e) { } } else { // Place the CellID here latLongString = "No location found"; } // This commented out line will include latitute and longtitute // myLocationText.setText("Your Phone is Currently at.. \n" + latLongString + "\n" + // addressString); myLocationText.setText("Your Phone is Currently at.. \n" + addressString); setAddress(addressString); ; } public void setAddress(String add) { this.addressString = add; } public String getAddress() { return addressString; } @Override protected boolean isRouteDisplayed() { return false; } }解决方案
I have used the following code in my app and its working absolutely fine for me. Try this. Hope this will help you.
// // Write the location name. // try { Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault()); List<Address> addresses = geo.getFromLocation(latitude, longitude, 1); if (addresses.isEmpty()) { yourtextfieldname.setText("Waiting for Location"); } else { if (addresses.size() > 0) { yourtextfieldname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName()); //Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show(); } } } catch (Exception e) { e.printStackTrace(); // getFromLocation() may sometimes fail }
更多推荐
获取地图地址或位置的地址Android中
发布评论