1. AndroidManifest.xml设置 权限与写入key值
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" />
2.获取 当前的所在坐标与名称
public abstract class GoogleMapLocation { Timer timer1; LocationManager lm; boolean gps_enabled = false; boolean network_enabled = false; String provide = null; private Context context; Geocoder mCoder; public GoogleMapLocation(Context context){ this.context=context; mCoder=new Geocoder(context, Locale.KOREA); getLocation(); } /** * 위치를 가져온다 * @return: true가져오기 성공,false;실패 */ public boolean getLocation() { //I use LocationResult callback class to pass location value from MyLocation to user code. if (lm == null) lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //exceptions will be thrown if provider is not permitted. try { gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) { } try { network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) { } //don't start listeners if no provider is enabled if (!gps_enabled && !network_enabled) return false; if (gps_enabled) { try { lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } provide = "gps"; } if (network_enabled) { try { lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } provide = "network"; } timer1 = new Timer(); timer1.schedule(new GetLastLocation(), 10); return true; } LocationListener locationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { timer1.cancel(); gotLocation(location); try { lm.removeUpdates(this); lm.removeUpdates(locationListenerNetwork); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } }; LocationListener locationListenerNetwork = new LocationListener() { public void onLocationChanged(Location location) { timer1.cancel(); gotLocation(location); try { lm.removeUpdates(this); lm.removeUpdates(locationListenerGps); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } }; class GetLastLocation extends TimerTask { @Override public void run() { try { lm.removeUpdates(locationListenerGps); lm.removeUpdates(locationListenerNetwork); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } Location net_loc = null, gps_loc = null; try { if (gps_enabled) gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (network_enabled) net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } catch (SecurityException e) { Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); } //if there are both values use the latest one if (gps_loc != null && net_loc != null) { if (gps_loc.getTime() > net_loc.getTime()) gotLocation(gps_loc); else gotLocation(net_loc); return; } Message messag=new Message(); if (gps_loc != null) { messag.obj=gps_loc; handler.sendMessage(messag); return; } if (net_loc != null) { messag.obj=net_loc; handler.sendMessage(messag); return; } messag.obj=null; handler.sendMessage(messag); } } private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Location location= (Location) msg.obj; gotLocation(location); locationAddr(location); } }; /** * 위치 내보내기 * @param location:위치 값 */ public abstract void gotLocation(Location location); /** * 주소 명칭 반환 * @param addr */ public abstract void getLocaionAddr(String addr); private String locationAddr(Location location){ String sLocationInfo=""; if (location!=null){ try { List<Address> addresses=mCoder.getFromLocation(location.getLatitude(),location.getLongitude(),1); if (addresses!=null){ Address addr=addresses.get(0); for (int i = 0; i <= addr.getMaxAddressLineIndex(); i++) { String addLine = addr.getAddressLine(i); sLocationInfo += String.format("%s", addLine); } getLocaionAddr(sLocationInfo); } }catch (IOException e){ } } return sLocationInfo; } }
3.显示到 GoogleMap上并移动到 当前所在位置
>如果实现 OnMapReadyCallback的话,当地图准备完以后就会进入onMapReady
>SupportMapFragment语句中 把googlemap添加到 指定的id上, 也就是 R.id.map
R.id.map是一个 fragment
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap; @Overrideprotected 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); }
@Overridepublic void onMapReady(GoogleMap googleMap) { mMap = googleMap; LocationResult locationResult = new LocationResult() { @Override public void gotLocation(Location location) { String msg = "lon: "+location.getLongitude()+" -- lat: "+location.getLatitude(); Log.i("map", msg); drawMarker(location); } }; MyLocation myLocation = new MyLocation(); myLocation.getLocation(getApplicationContext(), locationResult);}
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="mipl.amia.com.googlemapsample.MapsActivity" />
没有评论:
发表评论