티스토리 뷰

Android/실전 TIP

구글맵 현재 위치 표시

Dev-Drake 2019. 3. 25. 15:44
반응형
// Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pkg.LocationShare"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
 
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LocationShare"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <uses-library android:name="com.google.android.maps" />
 
    </application>
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    
</manifest>
 
 
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:apiKey="API키"
android:clickable="true"
/>
</LinearLayout>
 
 
// LocationShare.java
public class LocationShare extends MapActivity {
MapView mMap;
MyLocationOverlay2 mLocation;
 
protected boolean isRouteDisplayed() {
return false;
}
 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
mMap = (MapView)findViewById(R.id.mapview);
MapController mapControl = mMap.getController();
mapControl.setZoom(13);
mMap.setBuiltInZoomControls(true);
 
GeoPoint pt = new GeoPoint(37881311, 127729968);
mapControl.setCenter(pt);
 
mLocation = new MyLocationOverlay2(this, mMap); 
List<Overlay> overlays = mMap.getOverlays();
overlays.add(mLocation);
 
mLocation.runOnFirstFix(new Runnable() {
public void run() {
mMap.getController().animateTo(mLocation.getMyLocation());
}
});
}
 
public void onResume() {
super.onResume();
mLocation.enableMyLocation();
mLocation.enableCompass();
}
 
public void onPause() {
super.onPause();
mLocation.disableMyLocation();
mLocation.disableCompass();
}
 
class MyLocationOverlay2 extends MyLocationOverlay {
public MyLocationOverlay2(Context context, MapView mapView) {
super(context, mapView);
}
 
protected boolean dispatchTap() {
Toast.makeText(LocationShare.this, "여기가 현재 위치입니다.", 
Toast.LENGTH_SHORT).show();
return false;
}
}
}
 
반응형
댓글
반응형
최근에 올라온 글
Total
Today
Yesterday