简体   繁体   English

Android Google Map v2-在自定义标记上显示地图当前位置的推荐方法是什么?

[英]Android Google Map v2 - What is the recommended way to show current location on a map along with custom markers?

What is the recommended way to show current location on a map along with custom markers? 建议使用哪种方式在地图上显示当前位置以及自定义标记? The code below is working fine, that is, it is showing my current location and also showing a marker near my location that i added. 下面的代码工作正常,也就是说,它显示了我的当前位置,并且还在我添加的位置附近显示了一个标记。

Layout: activity_main.xml 布局: activity_main.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"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/tv_location"
    class="com.google.android.gms.maps.SupportMapFragment" />

AndroidManifest.xml AndroidManifest.xml中

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.geolocs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <!-- 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"/>  
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />    

    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.google.android.maps.v2.API_KEY"
    android:value="MY API KEY"/>
    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

MainActivity.java MainActivity.java

    import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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 MainActivity extends FragmentActivity implements LocationListener {

    GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();

        }else { // Google Play Services are available

            // Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();

            // Enabling MyLocation Layer of Google Map
            googleMap.setMyLocationEnabled(true);

            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);

            if(location!=null){
                onLocationChanged(location);
            }
            locationManager.requestLocationUpdates(provider, 20000, 0, this);

            //add a custom marker
            googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(63.813293,20.308319))
                    .title("House Rent"));
        }
    }

    @Override
    public void onLocationChanged(Location location) {

        TextView tvLocation = (TextView) findViewById(R.id.tv_location);

        // Getting latitude of the current location
        double latitude = location.getLatitude();

        // Getting longitude of the current location
        double longitude = location.getLongitude();

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(16));

        // Setting latitude and longitude in the TextView tv_location
        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

My questions are, Is it a recommended (or atleast a correct) way? 我的问题是,这是推荐的(或至少是正确的)方法吗? Is there any major issue that I have to rectify (or take care) in my MainActivity? 我必须在MainActivity中纠正(或注意)任何重大问题吗? Do I always need to extend FragmentActivity and implement LocationListener when displaying current location on a map with custom markers? 使用自定义标记在地图上显示当前位置时,我是否总是需要扩展FragmentActivity并实现LocationListener? Should I always add my custom markers inside onCreate() method (There are many example where it is inside onLocationChange()? Would highly appreciate some feedback. 我是否应该始终在onCreate()方法内添加自定义标记(很多示例都位于onLocationChange()内?非常感谢您提供一些反馈。

Is there any major issue that I have to rectify (or take care) in my MainActivity? No it looks fine to me. 不,对我来说很好。

Do I always need to extend FragmentActivity and implement LocationListener when displaying current location on a map with custom markers? No, you do not need to do either. 不,您也不需要这样做。 You just need to create a LocationListener object and obtain a reference to the map fragment you wish to use. 您只需要创建一个LocationListener对象并获取对您要使用的地图片段的引用。

Should I always add my custom markers inside onCreate() method (There are many example where it is inside onLocationChange()? It depends on what you are trying to do, but using a method like onLocationChange() to move a markers is good practice if you are doing something that involves moving the marker every time the location changes. If the markers you're creating are not intended to move and they only need to be created once then the onCreate() method is a good place to do it. Should I always add my custom markers inside onCreate() method (There are many example where it is inside onLocationChange()?这取决于您要执行的操作,但是使用类似onLocationChange()的方法来移动标记是一种好习惯如果您要进行的操作涉及每次位置更改时都移动标记,如果要创建的标记不打算移动并且只需要创建一次,那么onCreate()方法就是一个不错的选择。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM