开发者

How update user's current location in google map

开发者 https://www.devze.com 2023-03-06 17:38 出处:网络
Hi Friends I want to display开发者_运维技巧 user\'s current locationin Google map and add marker of his location in the mapand how can i Update their location in google map after every second ..???myL

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 ..???


myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 
    myLocationOverlay.enableMyLocation(); 
    myLocationOverlay.runOnFirstFix(addfirststandort);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消