为什么在Android Studio中我的当前位置城市为null?

huangapple 未分类评论48阅读模式
英文:

why the city of my current location is null in android studio?

问题

Location mLastLocation = locationResult.getLastLocation();
lat = mLastLocation.getLatitude() + "";
lon = mLastLocation.getLongitude() + "";
Geocoder gcd = new Geocoder(MLocationListener.this, Locale.getDefault());

List<Address> addresses;
try {
    addresses = gcd.getFromLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude(), 1);
    if (addresses.size() > 0) {
        // Log.d("city", addresses.get(0).getLocality());
        String cityName = addresses.get(0).getLocality();
        //Toast.makeText(MLocationListener.this, cityName, Toast.LENGTH_SHORT).show();
    }
} catch (IOException e) {
    e.printStackTrace();
}
英文:

hi I made a simple app that gets current location with gps and shows the current city but when I run it, it shows latitude and longitude correctly but returns city as null

Location mLastLocation = locationResult.getLastLocation();
lat = mLastLocation.getLatitude() + &quot;&quot;;
lon = mLastLocation.getLongitude() + &quot;&quot;;
Geocoder gcd = new Geocoder(MLocationListener.this, Locale.getDefault());

List&lt;Address&gt; addresses;
try {
    addresses = gcd.getFromLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude(), 1);
    if (addresses.size() &gt; 0) {
        // Log.d(&quot;city&quot;, addresses.get(0).getLocality());
        String cityName = addresses.get(0).getLocality();
        //Toast.makeText(MLocationListener.this, cityName, Toast.LENGTH_SHORT).show();
    }
} catch (IOException e) {
    e.printStackTrace();
}

答案1

得分: 0

请尝试以下解决方案:

GoogleMap map = googleMap;
this.map = googleMap;
this.map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
this.map.getUiSettings().setZoomControlsEnabled(true);
this.map.getUiSettings().setCompassEnabled(true);
this.map.getUiSettings().setMyLocationButtonEnabled(true);
this.map.getUiSettings().setZoomGesturesEnabled(true);
this.map.getUiSettings().setRotateGesturesEnabled(true);
String s = "纽约";
List<Address> addressList = null;
if(s != null){ 
    Geocoder geocoder = new Geocoder( ); // 在这里放入 "getActivity()" 或者 "this"
    try{
        addressList = geocoder.getFromLocationName(s, 1);
    } catch(IOException e){
        e.printStackTrace();
    }
    Address address = addressList.get(0);
    LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
    map.addMarker(new MarkerOptions().position(latLng).title(s));
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}

建议将这种类型的解决方案放在实现了 OnMapReadyCallback 接口的类中,放在 OnMapReady(GoogleMap googleMap) 方法内。

希望这对您有帮助。

英文:

Try this solution:

Google map = googleMap;
this.map = googleMap;
this.map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
this.map.getUiSettings().setZoomControlsEnabled(true);
this.map.getUiSettings().setCompassEnabled(true);
this.map.getUiSettings().setMyLocationButtonEnabled(true);
this.map.getUiSettings().setZoomGesturesEnabled(true);
this.map.getUiSettings().setRotateGesturesEnabled(true);
String s = &quot;New York&quot;; 
List&lt;Address&gt; addressList = null;
if(s!=null){ 
    Geocoder geocoder = new Geocoder( ); //inside put &quot;getActivity()&quot; or &quot;this&quot;
    try{
        addressList=geocoder.getFromLocationName(s, 1);
    }catch(IOException e){
        e.printStackTrace();
    }
    Address address = addressList.get(0);
    LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
    map.addMarker(new MarkerOptions().position(latLng).title(s));
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}

Is good to put this type of solution inside a class which implements OnMapReadyCallback, inside the method OnMapReady(GoogleMap googleMap)

Hope this can help you

huangapple
  • 本文由 发表于 2020年5月19日 19:50:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61890328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定