AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapapi">
<!--지도 API 사용을 위한 퍼미션과 앱 키-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<meta-data android:name="com.kakao.sdk.AppKey" android:value="나의api키"/>
<!--좌표 받아오는데 필요-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:networkSecurityConfig="@xml/network_security_config"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!-- URL scheme 사용을 위한 코드 -->
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="test2" android:scheme="bill" />
</intent-filter>
</activity>
</application>
</manifest>
network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
//http 네트워크 통신에 대한 예외처리
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">maps.daum-img.net</domain>
</domain-config>
</network-security-config>
ActivityMain.java
package com.example.mapapi;
public class MainActivity extends AppCompatActivity {
private LocationListener locationListener;
private LocationManager locationManager;
private static final int REQUEST_CODE_LOCATION = 2;
Context context = this;
Activity activity = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//지도 뷰 출력
MapView mapView = new MapView(this);
mapView.setDaumMapApiKey("d670b26983506ee500176859a3c0e024");
ViewGroup mapViewContainer = (ViewGroup) findViewById(R.id.map_view);
//위도 경도를 변수로 설정
MapPoint mapPoint = MapPoint.mapPointWithGeoCoord( 37.5514579595,126.951949155);
//중앙에 표시될 위치 설정 / true,false = 애니메이션 효과
mapView.setMapCenterPoint(mapPoint, true);
//맵뷰를 컨테이너에 적용
mapViewContainer.addView(mapView);
//포인트 마커
MapPOIItem marker = new MapPOIItem();
marker.setItemName("한세사이버보안고등학교");
marker.setTag(0);
// 기본으로 제공하는 BluePin 마커 모양.
marker.setMapPoint(mapPoint);
// 마커를 클릭했을때, 기본으로 제공하는 RedPin 마커 모양.
marker.setMarkerType(MapPOIItem.MarkerType.BluePin);
marker.setSelectedMarkerType(MapPOIItem.MarkerType.RedPin);
//마커적용
mapView.addPOIItem(marker);
}
double latitude = 0;
double longitude = 0;
public void btn_Click(View view) {
// 사용자의 위치 수신 //
settingGPS();
// 사용자의 현재 위치 //
Location userLocation = getMyLocation();
if( userLocation != null ) {
// TODO 위치를 처음 얻어왔을 때 하고 싶은 것
latitude = userLocation.getLatitude();
longitude = userLocation.getLongitude();
}
}
//사용자 위치수신
private Location getMyLocation() {
Location currentLocation = null;
// Register the listener with the Location Manager to receive location updates
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// 사용자 권한 요청
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, this.REQUEST_CODE_LOCATION);
}
else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
// 수동으로 위치 구하기
String locationProvider = LocationManager.GPS_PROVIDER;
currentLocation = locationManager.getLastKnownLocation(locationProvider);
if (currentLocation != null) {
double lng = currentLocation.getLongitude();
double lat = currentLocation.getLatitude();
Log.d("Main", "longtitude=" + lng + ", latitude=" + lat);
}
}
return currentLocation;
}
private void settingGPS() {
// Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
// TODO 위도, 경도로 하고 싶은 것
// 카카오 API URL Scheme 사용
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("daummaps://search?q=컴퓨터수리&p="+latitude+","+longitude));
startActivity(intent);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
}
boolean canReadLocation = false;
public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {
if (requestCode == this.REQUEST_CODE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// success!
Location userLocation = getMyLocation();
if( userLocation != null ) {
// 다음 데이터 //
//String lang = MString.getLangFromSystemLang(getApplicationContext());
// todo 사용자의 현재 위치 구하기
double latitude = userLocation.getLatitude();
double longitude = userLocation.getLongitude();
}
canReadLocation = true;
} else {
// Permission was denied or request was cancelled
canReadLocation = false;
}
}
}
}
위와같이 카카오 맵 API 사용하는 소스를 구해서 예제로 만들었습니다. 핸드폰으로 연결하여 실행시키니 빌드에는 문제가 없으나, 앱이 설치되지 않습니다. 앱 작동도 정상적으로 이루어지는데 핸드폰에 설치만 안되어있네요... 다른 프로젝트들을 열어서 실행하면 앱이 정상적으로 설치되는것을 보아 핸드폰이나 안드로이드 스튜디오 자체의 문제는 아닌것같은데 해결방법이 있을까요??