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
如何使用谷歌API客户端在谷歌地图Android中显示我的当前位置_Android_Google Maps - Fatal编程技术网

如何使用谷歌API客户端在谷歌地图Android中显示我的当前位置

如何使用谷歌API客户端在谷歌地图Android中显示我的当前位置,android,google-maps,Android,Google Maps,嗨,我是android新手,在我的应用程序中,我想使用GoogleApi客户端显示我的当前位置,为此我编写了以下代码,但当前位置未显示 地图显示如我下面的屏幕截图 有人能帮我一下吗?有什么问题吗 活动:- 舱单:- 试试这个: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.

嗨,我是android新手,在我的应用程序中,我想使用GoogleApi客户端显示我的当前位置,为此我编写了以下代码,但当前位置未显示

地图显示如我下面的屏幕截图

有人能帮我一下吗?有什么问题吗

活动:- 舱单:-

试试这个:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);



    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    markers = new Hashtable<String, String>();

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();




}

@Override
protected void onStart() {
    googleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    googleApiClient.disconnect();
    super.onStop();
}

//Getting current location
private void getCurrentLocation() {
    mMap.clear();
    //Creating a location object
    if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null) {
        //Getting longitude and latitude
        longitude = location.getLongitude();
        latitude = location.getLatitude();



        //moving the map to location
        //moveMap();
    }
}

//Function to move the map
private void moveMap() {
    //String to display current latitude and longitude
    String msg = latitude + ", " + longitude;

    //Creating a LatLng Object to store Coordinates
    LatLng latLng = new LatLng(latitude, longitude);


    //Adding marker to map
    mMap.addMarker(new MarkerOptions()
            .position(latLng) //setting position
            .draggable(false) //Making the marker draggable
            .title("Current Location")
    .visible(true)); //Adding a title


    //Moving the camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    //Animating the camera
    mMap.animateCamera(CameraUpdateFactory.zoomTo(18));

    //Displaying current coordinates in toast
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng latLng = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.setOnMarkerDragListener(this);
    mMap.setOnMapLongClickListener(this);
}

@Override
public void onConnected(Bundle bundle) {
    getCurrentLocation();
    moveMap();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onMapLongClick(LatLng latLng) {
    //Clearing all the markers
  /*  mMap.clear();

    //Adding a new marker to the current pressed position
    mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .draggable(false));*/

}

@Override
public void onMarkerDragStart(Marker marker) {

}

@Override
public void onMarkerDrag(Marker marker) {

}

@Override
public void onMarkerDragEnd(Marker marker) {
    //Getting the coordinates
    latitude = marker.getPosition().latitude;
    longitude = marker.getPosition().longitude;

    //Moving the map
    moveMap();
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
如果(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
}
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
markers=新哈希表();
GoogleapClient=新的GoogleapClient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
}
@凌驾
受保护的void onStart(){
googleApiClient.connect();
super.onStart();
}
@凌驾
受保护的void onStop(){
googleApiClient.disconnect();
super.onStop();
}
//获取当前位置
私有void getCurrentLocation(){
mMap.clear();
//创建位置对象
if(ActivityCompat.checkSelfPermission(this,ACCESS\u FINE\u LOCATION)!=PackageManager.PERMISSION\u已授予&ActivityCompat.checkSelfPermission(this,ACCESS\u rough\u LOCATION)!=PackageManager.PERMISSION\u已授予){
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
Location Location=LocationServices.FusedLocationApi.getLastLocation(GoogleAppClient);
如果(位置!=null){
//获取经纬度
longitude=location.getLongitude();
纬度=位置。getLatitude();
//将地图移动到指定位置
//moveMap();
}
}
//函数来移动地图
私有void moveMap(){
//显示当前纬度和经度的字符串
字符串msg=纬度+“,”+经度;
//创建板条对象以存储坐标
LatLng LatLng=新LatLng(纬度、经度);
//向地图添加标记
mMap.addMarker(新标记选项()
.位置(车床)//设置位置
.draggable(false)//使标记可拖动
.标题(“当前位置”)
.visible(true));//添加标题
//移动摄像机
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//设置摄影机的动画
mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
//在toast中显示当前坐标
Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
LatLng LatLng=新LatLng(-34151);
mMap.addMarker(新的MarkerOptions().position(latLng).draggable(true));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.setOnMarkerDragListener(此);
mMap.setOnMapLongClickListener(这个);
}
@凌驾
未连接的公共空间(捆绑包){
getCurrentLocation();
moveMap();
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
}
@凌驾
在MaplongClick(LatLng LatLng)上的公共无效{
//清除所有标记
/*mMap.clear();
//将新标记添加到当前按下的位置
mMap.addMarker(新标记选项()
.位置(车床)
.draggable(false))*/
}
@凌驾
MarkerDragStart上的公共空白(标记){
}
@凌驾
MarkerDrag上的公共空白(标记){
}
@凌驾
MarkerDragend上的公共空白(标记){
//获取坐标
纬度=marker.getPosition().纬度;
经度=marker.getPosition().longitude;
//移动地图
moveMap();
}
试试这个:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);



    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    markers = new Hashtable<String, String>();

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();




}

@Override
protected void onStart() {
    googleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    googleApiClient.disconnect();
    super.onStop();
}

//Getting current location
private void getCurrentLocation() {
    mMap.clear();
    //Creating a location object
    if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null) {
        //Getting longitude and latitude
        longitude = location.getLongitude();
        latitude = location.getLatitude();



        //moving the map to location
        //moveMap();
    }
}

//Function to move the map
private void moveMap() {
    //String to display current latitude and longitude
    String msg = latitude + ", " + longitude;

    //Creating a LatLng Object to store Coordinates
    LatLng latLng = new LatLng(latitude, longitude);


    //Adding marker to map
    mMap.addMarker(new MarkerOptions()
            .position(latLng) //setting position
            .draggable(false) //Making the marker draggable
            .title("Current Location")
    .visible(true)); //Adding a title


    //Moving the camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    //Animating the camera
    mMap.animateCamera(CameraUpdateFactory.zoomTo(18));

    //Displaying current coordinates in toast
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng latLng = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.setOnMarkerDragListener(this);
    mMap.setOnMapLongClickListener(this);
}

@Override
public void onConnected(Bundle bundle) {
    getCurrentLocation();
    moveMap();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onMapLongClick(LatLng latLng) {
    //Clearing all the markers
  /*  mMap.clear();

    //Adding a new marker to the current pressed position
    mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .draggable(false));*/

}

@Override
public void onMarkerDragStart(Marker marker) {

}

@Override
public void onMarkerDrag(Marker marker) {

}

@Override
public void onMarkerDragEnd(Marker marker) {
    //Getting the coordinates
    latitude = marker.getPosition().latitude;
    longitude = marker.getPosition().longitude;

    //Moving the map
    moveMap();
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
如果(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
}
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
markers=新哈希表();
GoogleapClient=新的GoogleapClient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
}
@凌驾
受保护的void onStart(){
googleApiClient.connect();
super.onStart();
}
@凌驾
受保护的void onStop(){
googleApiClient.disconnect();
super.onStop();
}
//获取当前位置
私有void getCurrentLocation(){
mMap.clear();
//创建位置对象
if(ActivityCompat.checkSelfPermission(this,ACCESS\u FINE\u LOCATION)!=PackageManager.PERMISSION\u已授予&ActivityCompat.checkSelfPermission(this,ACCESS\u rough\u LOCATION)!=PackageManager.PERMISSION\u已授予){
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);



    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    markers = new Hashtable<String, String>();

    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();




}

@Override
protected void onStart() {
    googleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    googleApiClient.disconnect();
    super.onStop();
}

//Getting current location
private void getCurrentLocation() {
    mMap.clear();
    //Creating a location object
    if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null) {
        //Getting longitude and latitude
        longitude = location.getLongitude();
        latitude = location.getLatitude();



        //moving the map to location
        //moveMap();
    }
}

//Function to move the map
private void moveMap() {
    //String to display current latitude and longitude
    String msg = latitude + ", " + longitude;

    //Creating a LatLng Object to store Coordinates
    LatLng latLng = new LatLng(latitude, longitude);


    //Adding marker to map
    mMap.addMarker(new MarkerOptions()
            .position(latLng) //setting position
            .draggable(false) //Making the marker draggable
            .title("Current Location")
    .visible(true)); //Adding a title


    //Moving the camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    //Animating the camera
    mMap.animateCamera(CameraUpdateFactory.zoomTo(18));

    //Displaying current coordinates in toast
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng latLng = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.setOnMarkerDragListener(this);
    mMap.setOnMapLongClickListener(this);
}

@Override
public void onConnected(Bundle bundle) {
    getCurrentLocation();
    moveMap();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onMapLongClick(LatLng latLng) {
    //Clearing all the markers
  /*  mMap.clear();

    //Adding a new marker to the current pressed position
    mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .draggable(false));*/

}

@Override
public void onMarkerDragStart(Marker marker) {

}

@Override
public void onMarkerDrag(Marker marker) {

}

@Override
public void onMarkerDragEnd(Marker marker) {
    //Getting the coordinates
    latitude = marker.getPosition().latitude;
    longitude = marker.getPosition().longitude;

    //Moving the map
    moveMap();
}
private void locateMyLocation(Location currentLocation) {
    mGoogleMap.addMarker(new MarkerOptions()
            .title(mContext.getResources().getString(R.string.map_user_location_title))
            .anchor(0.0f, 1.0f)
            .position(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude())));
    mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
    mGoogleMap.getUiSettings().setZoomControlsEnabled(false);
    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 15.0f));
}