Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 显示设备谷歌地图的当前位置(片段)_Android_Google Maps_Android Fragments_Google Maps Android Api 2_Google Maps Api 2 - Fatal编程技术网

Android 显示设备谷歌地图的当前位置(片段)

Android 显示设备谷歌地图的当前位置(片段),android,google-maps,android-fragments,google-maps-android-api-2,google-maps-api-2,Android,Google Maps,Android Fragments,Google Maps Android Api 2,Google Maps Api 2,我尝试了很多不同的解决方案,我也在谷歌上搜索,但仍然没有得到所需的解决方案。这个问题还没有解决。我有一些片段的ViewPager。在第一个片段中,我想使用GoogleMapAPIv2来显示设备的当前位置。如果我使用纬度/经度的当前值并缩放它,效果会很好。了解片段中当前位置的当前纬度和经度的最佳方法是什么(不是片段活动或活动)?我只是想知道它是否可能是碎片?如果有人有同样的问题并解决了,请告诉我 谢谢你的帮助 下面是我用来设置地图的代码: fragment.xml <RelativeLayo

我尝试了很多不同的解决方案,我也在谷歌上搜索,但仍然没有得到所需的解决方案。这个问题还没有解决。我有一些片段的ViewPager。在第一个片段中,我想使用GoogleMapAPIv2来显示设备的当前位置。如果我使用纬度/经度的当前值并缩放它,效果会很好。了解片段中当前位置的当前纬度和经度的最佳方法是什么(不是片段活动或活动)?我只是想知道它是否可能是碎片?如果有人有同样的问题并解决了,请告诉我

谢谢你的帮助

下面是我用来设置地图的代码:

fragment.xml

<RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/map_container">

      <!-- The map fragments will go here -->
</RelativeLayout>

用于当前位置..尝试使用此..

    LocationManager lm;
    Location location;
    Geocoder geo;

    //convert lat, long to address.
    List<Address> addresses;
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //get the last known latitude longitude from network
    location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    //create an instance of geocoder
    geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
    try
    {
        addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if(addresses.get(0).getPostalCode() != null)
        {
            //You can get Lat, Long or Zip code
        }
    }
    catch (Exception e)
    {

    }
LocationManager-lm;
位置;
地理编码器;
//将lat、long转换为地址。
列出地址;
lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
//从网络中获取最后一个已知的经纬度
location=lm.getLastKnownLocation(LocationManager.NETWORK\u提供程序);
//创建geocoder的实例
geo=新的地理编码器(this.getApplicationContext(),Locale.getDefault());
尝试
{
地址=geo.getFromLocation(location.getLatitude(),location.getLatitude(),1);
if(addresses.get(0).getPostalCode()!=null)
{
//您可以获取Lat、Long或邮政编码
}
}
捕获(例外e)
{
}

可能是“片段”…您能展示您的XML吗?您能仔细查看帖子吗。我添加了xml。如果您是在片段或活动中执行它,那又有什么关系呢?我不明白意外情况是什么…嘿,伙计,这是开玩笑吗?仔细检查我的帖子。我觉得你好像不明白我的问题。我想知道设备的当前位置,但我发现的所有材料都使用活动或碎片活动,这些活动在碎片中无法正常工作。所以我想知道在我的情况下如何正确地做到这一点。你有什么想法吗?又来了!我看到你更新的代码,我有一些问题。您认为在使用前检查服务的连接是否更好?在哪里使用代码更好?
    LocationManager lm;
    Location location;
    Geocoder geo;

    //convert lat, long to address.
    List<Address> addresses;
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //get the last known latitude longitude from network
    location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    //create an instance of geocoder
    geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
    try
    {
        addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if(addresses.get(0).getPostalCode() != null)
        {
            //You can get Lat, Long or Zip code
        }
    }
    catch (Exception e)
    {

    }