如何更新用户在谷歌地图中的当前位置

发布于 2024-11-07 10:36:40 字数 76 浏览 0 评论 0原文

嗨朋友们 我想在谷歌地图中显示用户的当前位置,并在地图中添加他的位置标记,以及如何每秒更新他们在谷歌地图中的位置..???

Hi Friends
I want to display user's current location in Google map and add marker of his location in the map and how can i Update their location in google map after every second ..???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2024-11-14 10:36:40
myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 
    myLocationOverlay.enableMyLocation(); 
    myLocationOverlay.runOnFirstFix(addfirststandort);
myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 
    myLocationOverlay.enableMyLocation(); 
    myLocationOverlay.runOnFirstFix(addfirststandort);
不交电费瞎发啥光 2024-11-14 10:36:40

java 文件

public class MYGIRNE extends FragmentActivity {

    GoogleMap map;

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


        SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting Map for the SupportMapFragment
        map = fm.getMap();

        if(map!=null){

            // Enable MyLocation Button in the Map
            map.setMyLocationEnabled(true);
         LocationManager manager=(LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria criteria=new Criteria();
            String provider =manager.getBestProvider(criteria, true);
            Location cloc=manager.getLastKnownLocation(provider);
            double lat=0;
            double lng =0;
             lat =cloc.getLatitude();
             lng= cloc.getLongitude();
            LatLng latlng = new LatLng (lat,lng);
            map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
           map.animateCamera(CameraUpdateFactory.zoomTo(15));
           map.addMarker(new MarkerOptions()
             .title("Your Current Location")
            .position(latlng));
            .icon(BitmapDescriptorFactory.defaultMarker(
                      BitmapDescriptorFactory.HUE_AZURE)));
}
}

xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.firsttry.cyptour.MYGIRNE" >

    <fragment
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

还在您的清单文件中包含以下内容

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="enter your APIKEY" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

此代码应该是应用程序的一个元素

然后将其包含在清单文件的使用权限下

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!-- External storage for caching. -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- My Location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

我希望这有帮助

java file

public class MYGIRNE extends FragmentActivity {

    GoogleMap map;

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


        SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting Map for the SupportMapFragment
        map = fm.getMap();

        if(map!=null){

            // Enable MyLocation Button in the Map
            map.setMyLocationEnabled(true);
         LocationManager manager=(LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria criteria=new Criteria();
            String provider =manager.getBestProvider(criteria, true);
            Location cloc=manager.getLastKnownLocation(provider);
            double lat=0;
            double lng =0;
             lat =cloc.getLatitude();
             lng= cloc.getLongitude();
            LatLng latlng = new LatLng (lat,lng);
            map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
           map.animateCamera(CameraUpdateFactory.zoomTo(15));
           map.addMarker(new MarkerOptions()
             .title("Your Current Location")
            .position(latlng));
            .icon(BitmapDescriptorFactory.defaultMarker(
                      BitmapDescriptorFactory.HUE_AZURE)));
}
}

xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.firsttry.cyptour.MYGIRNE" >

    <fragment
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

also include the following in your manifest file

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="enter your APIKEY" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

this code should be an element of application

then include this under the uses permission of your manifest file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!-- External storage for caching. -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- My Location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

i hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文