简体   繁体   English

要填写我的位置我需要填写什么? 就像Google的蓝点一样

[英]What to I need to fill to get my location? Like the blue dot google has

I am new to using Google Maps API and have implemented the map in the fragment. 我是使用Google Maps API的新手,并且已在片段中实现了地图。 I have added a marker that on the map. 我在地图上添加了一个标记。 Now i want my location to pop up like google maps have, the blue dot. 现在,我希望我的位置像谷歌地图上的蓝点一样弹出。 Can anyone help me finish this? 谁能帮我完成这个任务? Thanks alot. 非常感谢。

My java file: 我的Java文件:

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements     OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng some = new LatLng(80.153, 15.2620);
    mMap.addMarker(new MarkerOptions().position(some).title("Marker in some"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(some, 18));

}
}

Manifest file have this ACCESS_FINE_LOCATION 清单文件具有此ACCESS_FINE_LOCATION

You need to enable My Location Layer . 您需要启用“ 我的位置图层”

  1. Check your app get the Permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION 检查您的应用是否获得权限ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION

  2. Enable the My Location layer , use mMap.setMyLocationEnabled(true); 启用“ 我的位置”图层 ,使用mMap.setMyLocationEnabled(true);

If you don't do step 1, you would get SecurityException. 如果不执行步骤1,则将收到SecurityException。 You should override ActivityCompat.OnRequestPermissionsResultCallback So the final code should look like this: 您应该重写ActivityCompat.OnRequestPermissionsResultCallback因此最终代码应如下所示:

// Check for permission to access Location
private boolean checkLocationPermission() {
    Log.d(TAG, "checkPermission()");
    // Ask for permission if it wasn't granted yet
    return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED);
}

// Asks for permissions
private void askPermission() {
    Log.d(TAG, "askPermission()");
    ActivityCompat.requestPermissions(
            this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
    );
}

// Reaction to whether user granted or denied permissions
@Override
public void onRequestPermissionsResult(
        int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Log.d(TAG, "onRequestPermissionsResult()");
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted
                if (checkLocationPermission())
                    mMap.setMyLocationEnabled(true);
            } else {
                // Permission denied
                // TODO
            }
            break;
        }
    }
}

and do this in onMapReady : 并在onMapReady执行此onMapReady

    if (checkLocationPermission())
        mMap.setMyLocationEnabled(true);
    else
        askPermission();

Here is the reference: 这里是参考:

https://developers.google.com/maps/documentation/android-api/location#my-location https://developers.google.com/maps/documentation/android-api/location#my-location

https://developers.google.com/maps/documentation/android-api/location#runtime-permission https://developers.google.com/maps/documentation/android-api/location#runtime-permission

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使蓝色的位置点出现在我的Android应用程序中? - How do I get the blue location dot to appear in my Android application? 如何使用 OSMDROID 创建像 Google Map 这样的方向蓝点? - How do I create a direction blue dot like Google Map using OSMDROID? Android Studio Google Maps无法定位或找到蓝点 - Android Studio Google Maps cannot get latlng or find the blue dot 如何获得正常的谷歌地图蓝色当前位置标记? - How can I get the normal Google Maps blue current location marker? 有没有办法在Android的谷歌地图上更改我的位置点的颜色? - Is there a way to change the color of my location's dot on google map in Android? 我现在需要更精确的位置,例如24.0,我想要24.1235152 - I need more accurate positon in my location now its like 24.0 and i want 24.1235152 Google地图如何快速找到我的位置,但是当我使用位置管理器时,我需要等待40秒以上 - How does Google maps find my location quickly but when I use Location Manager I need to wait 40+seconds Intellij IDEA中的蓝点(圆圈)是什么意思? - What does blue dot (circle) in Intellij IDEA mean? 如何获得我最后的谷歌地图位置? - How to get my last google map location? 如何使我的应用接收诸如Google Maps之类的位置信息 - How to make my app receive location intents like Google Maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM