【问题标题】:How to get current location in map in android?如何在android的地图中获取当前位置?
【发布时间】:2017-01-09 00:36:16
【问题描述】:

我正在尝试实现当前位置地图应用程序,我正在片段中加载地图,但它需要纬度和经度值来定位地址..如何实现当前位置功能来映射......提前致谢..

这是我的地图片段..

public class ContactFragment extends Fragment {
MapView mapView;
GoogleMap googleMap;
private WebView contactWebView;
public ContactFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_contact, container, false);
    getActivity().setTitle("Contact Us");
    contactWebView = (WebView)view.findViewById(R.id.contact_us);
    contactWebView.setBackgroundColor(0);
    if(Build.VERSION.SDK_INT >= 11){
        contactWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    }
    contactWebView.getSettings().setBuiltInZoomControls(true);
    AssetManager mgr = getContext().getAssets();
    String filename = null;
    try {
        filename = "contact.html";
        System.out.println("filename : " + filename);
        InputStream in = mgr.open(filename, AssetManager.ACCESS_BUFFER);
        String sHTML = StreamToString(in);
        in.close();
        contactWebView.loadDataWithBaseURL(null, sHTML, "text/html", "utf-8", null);
        //singleContent.setText(Html.fromHtml(sHTML));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mapView = (MapView) view.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    if (mapView != null) {
        googleMap = mapView.getMap();
        googleMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
                .anchor(0.5f, 1.0f)
                .position(new LatLng( 12.895960, 77.559921)));
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            MapsInitializer.initialize(this.getActivity());
            LatLng position = new LatLng(12.895960, 77.559921);
            CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);

            googleMap.moveCamera(updatePosition);
            //googleMap.animateCamera(updatePosition);

            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,16));
            return view;
        }
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        MapsInitializer.initialize(this.getActivity());
        //            LatLngBounds.Builder builder = new LatLngBounds.Builder();
 //            builder.include(new LatLng(12.8909537, 77.5594254));
 //            LatLngBounds bounds = builder.build();
        int padding = 0;
        LatLng position = new LatLng(12.895960, 77.559921);
        CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
        CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(8);
        googleMap.moveCamera(updatePosition);
        googleMap.animateCamera(updateZoom);



    }
    return view;
}
@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}
@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
}
@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}
@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}
public static String StreamToString(InputStream in) throws IOException {
    if(in == null) {
        return "";
    }
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
    }
    return writer.toString();
}



}

【问题讨论】:

标签: android google-maps android-layout android-studio android-fragments


【解决方案1】:
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        mMap.setMyLocationEnabled(true);

        if (mMap != null) {


         mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

       @Override
       public void onMyLocationChange(Location arg0) {
        // TODO Auto-generated method stub

         mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
       }
      });

        }
    }

【讨论】:

  • 它在mMap.setMyLocationEnabled(true)处询问权限检查错误;
  • 你在AndroidManifest.xml中添加了位置权限吗?
  • 是的,我在清单文件中添加了所有权限......请给我发送完整的片段代码修改......谢谢你
  • @PankajJadhav 你在 Android 6.0+ 上测试吗?
  • 这个答案没有回答问题。提供的代码用于在当前位置添加地图标记,而不是用于添加导航到当前位置的按钮“我的位置”!
【解决方案2】:

从 Google 的 documentation 来看,Android 设备可用的位置数据包括使用多种技术精确定位的设备的当前位置 - 移动方向和方法,以及设备是否已跨越预定义的地理区域边界或地理围栏。

根据您的应用需求,您可以选择 几种处理位置数据的方法:

  • 我的位置图层 提供了一种在地图上显示设备位置的简单方法。它不提供数据。

  • Google Play services Location API 推荐用于所有位置数据的程序化请求。

  • LocationSource 界面允许您提供自定义位置提供程序。

有关更多信息,请查看文档,它将向您解释您需要的一切以及这些教程和相关的 SO 问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多