Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 如何获取我的手机&x27;安卓系统中使用谷歌地图Api的当前位置?_Android_Location_Maps - Fatal编程技术网

Android 如何获取我的手机&x27;安卓系统中使用谷歌地图Api的当前位置?

Android 如何获取我的手机&x27;安卓系统中使用谷歌地图Api的当前位置?,android,location,maps,Android,Location,Maps,我想使用谷歌地图Api在我当前的位置上获得一个标记。我在安卓手机上测试了它,只得到了一张地图。我现在的位置没有标记。即使是我在代码中添加的祝酒词也没有显示出来。有人能帮我吗。 我在下面发布我的代码: public class MainActivity extends Activity implements LocationListener { GoogleMap map; @Override void onCreate(Bundle savedInstanceState) { super.on

我想使用谷歌地图Api在我当前的位置上获得一个标记。我在安卓手机上测试了它,只得到了一张地图。我现在的位置没有标记。即使是我在代码中添加的祝酒词也没有显示出来。有人能帮我吗。 我在下面发布我的代码:

public class MainActivity extends Activity implements LocationListener {
GoogleMap map;

@Override
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
}

 @Override
   public void onLocationChanged(Location location) {

 map.clear();

MarkerOptions mp = new MarkerOptions();

  mp.position(new LatLng(location.getLatitude(), location.getLongitude()));

  mp.title("my position");

   map.addMarker(mp);
   Toast.makeText(getApplicationContext(),
       location.getLatitude() +", "+location.getLongitude(),
       Toast.LENGTH_LONG).show();

  map.animateCamera(CameraUpdateFactory.newLatLngZoom(
   new LatLng(location.getLatitude(), location.getLongitude()), 16));

  }  }

如果您想使用谷歌地图获取位置,谷歌地图提供了一个内置方法

map.getLatitude();
map.getLongitude();

试试看,它会起作用。

您已经通过以下方法获得了位置:
location.getLatitude(),location.getLongitude()
打开移动GPS,当它停止闪烁时,表示它已获得位置,然后将显示标记。

只需删除
map.clear()并添加
标记。删除()

这对我有用。。试试这个

protected void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                //actionBar.getActionBar();



                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                System.out.println("current "+locationManager);

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 1, this);
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                addGoogleMap();
                addMarkers();

            }

            @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") 
            private void addGoogleMap() {
                if(googleMap == null){
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                googleMap.setOnMarkerClickListener(this);
                  googleMap.setOnMarkerDragListener(this);
                }

            }


            private void addMarkers() {

                if(googleMap != null)
                {
                lt = new LatLng(location.getLatitude(), location.getLongitude());


                marker = googleMap.addMarker(new MarkerOptions().position(lt)
                          .title("Current location ").snippet("Race Start: 9:00 AM CST")
                          .draggable(false));

                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                          lt, 20));
                }

            }

@Override
            public void onLocationChanged(Location location) {
            //txtLat = (TextView) findViewById(R.id.textview1);
            //txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
                System.out.println("loc changed");
                //googleMap.clear();
                marker.remove();
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                System.out.println("current "+locationManager);

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 1, this);
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                lt = new LatLng(location.getLatitude(), location.getLongitude());
                marker = googleMap.addMarker(new MarkerOptions().position(lt)
                          .title("Current location ").snippet("Race Start: 9:00 AM CST")
                          .draggable(false));
}

将所需权限添加到manifest.xmlI已将所有权限添加到manifest.=在您的示例中为“map”。我无法将这些方法用于map