개발언어/Xamarin

XamarinForms GoogleMap 연동

공장장코난 2021. 1. 31. 10:41

지난번에 Google Maps Platform에서 획득한 Api Key를 통해 GoogleMap 서비스를 연동하고자 한다. 
예제는 지정 GPS좌표에 Pin 설정하는 예제로 Youtube를 통해 공부해 본다.
참조:Xamarin Forms Maps Part 1 - YouTube

APIKEY 획득
codepulse.tistory.com/92

 

XamarinForms GoogleMap 연동 - API Key 획득

최근 Xamarin에서 GoogleMap 연동을 위한 공부를 진행중이다. YouTube를 통해 공부를 진행하는데, 바로 진행할 수 있는게 아니고 사전 작업이 필요하다. 첫번째로 GoogleMap API Key를 획득해야 한다. 공부한

codepulse.tistory.com

XamarinForms Project 생성 후 NugetPackageManager로부터 GoogleMaps Service PlugIn을 설치 한다.

>>NugetPackageManager를 통해 Xamarin.GoogleMaps 설치
>>Xamarin.Forms의 Version이 v4.0.0 이하인 경우 최신 버전으로 업그레이드 해야함


Android 매니페스트 설정, 아래 내용 활성화
-ACCESS_COARSE_LOCATION
-ACCESS_FINE_LOCATION
-ACCESS_MOCK_LOCATION
-INTERNET

Android Project 추가 PlugIn 설치
>>NugetPackageManager를 통해 Plugin.CurrentActivity  설치

MainActivity.cs
protected override void OnCreate(Bundle saveInstanceState)
{
   ...
   //Initialize for Xamarin.Forms.GoogleMaps
   Xamarin.FormsGoogleMaps.Init(this, saveInstanceState);
   LoadApplication(new App());

}

MainApplication.cs 추가
using Android.App;
using Android.Runtime;
using GoogleMapExamApp.Constants;
using Plugin.CurrentActivity;
using System;

#if DEBUG
[Application(Debuggable = true)]
#else
[Application(Debuggable = false)]
#endif

[MetaData("com.google.android.maps.v2.API_KEY", Value="GoogleMapsApiKey")]

public class MainApplication : Application
{
    public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
    {
        
    }

    public override void OnCreate()
    {
        base.OnCreate();
        CrossCurrentActivity.Current.Init(this);
    }
}

XamarinProject
MainPage.xaml
xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps"

<maps:Map x:name="googleMap"/>

MainPage.cs
using Xamarin.Forms;
using Xamarin.Forms.GoogleMaps;

Pin curPin = new Pin()
{
    Type = PinType.Place,
    Label = "HighSchool",
    Address = "Seo-gu, InCheon, Korea",
    Position = new Position(37.52910769193946, 126.65717902235926),
    Rotation = 33.3f,
    Tag = "id_incheon",
}

googleMap.Pins.Add(curPin);
googleMap.MoveToRegion(MapSpan.FromCenterAndRadius(curPin.Position, Distance.FromMeters(5000)));

※GPS좌표는 GoogleMap을 통해 획득 했다.

예제는 성공했으나, 다음번엔 GPS센서를 통해 내가 있는 위치를 가져올 계획이다.