반응형
소스 코드
MainWindow.xaml
<Window x:Class="카카오_API를_이용한_위치_검색.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:카카오_API를_이용한_위치_검색"
mc:Ignorable="d"
Title="카카오 API를 이용한 위치 검색" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="9*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock
HorizontalAlignment="Right"
VerticalAlignment="Center">검색어:</TextBlock>
<TextBox
Name="tbox_query"
VerticalAlignment="Center"
Margin="5,0,5,0"
Background="Gray"
Grid.Column="1"/>
<Button
Name="btn_search"
IsDefault="True"
Click="Button_Click"
VerticalAlignment="Center"
Grid.Column="2"
Content="검색"/>
<ListBox
Name="lbox_locale"
SelectionChanged="ListBox_SelectionChanged"
Background="Cyan"
Grid.Row="1"/>
<WebBrowser
Name="wb"
Source="http://ehpub.co.kr/kakaomap.html"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="3"/>
</Grid>
</Window>
MainWindow.cs
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace 카카오_API를_이용한_위치_검색
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
List<MyLocale> mls = KakaoAPI.Search(tbox_query.Text);
lbox_locale.ItemsSource = mls;
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(lbox_locale.SelectedIndex==-1)
{
return;
}
MyLocale ml = lbox_locale.SelectedItem as MyLocale;
object[] ps = new object[] { ml.Lat, ml.Lng };
wb.InvokeScript("setCenter", ps);
}
}
}
MyLocale.cs
namespace 카카오_API를_이용한_위치_검색
{
class MyLocale
{
internal string Name
{
get;
private set;
}
internal double Lat
{
get;
private set;
}
internal double Lng
{
get;
private set;
}
internal MyLocale(string name, double lat, double lng)
{
Name = name;
Lat = lat;
Lng = lng;
}
public override string ToString()
{
return Name;
}
}
}
KakaoAPI.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace 카카오_API를_이용한_위치_검색
{
static class KakaoAPI
{
internal static List<MyLocale> Search(string query)
{
List<MyLocale> mls = new List<MyLocale>();
string site = "https://dapi.kakao.com/v2/local/search/keyword.json";
string rquery = string.Format("{0}?query={1}", site, query);
WebRequest request = WebRequest.Create(rquery);
string rkey = "[자신의 Kakao REST API 키]";
string header = "KakaoAK " + rkey;
request.Headers.Add("Authorization",header);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
String json = reader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
dynamic dob = js.Deserialize<dynamic>(json);
dynamic docs = dob["documents"];
object[] buf = docs;
int length = buf.Length;
for(int i=0;i<length;i++)
{
string lname = docs[i]["place_name"];
double x = double.Parse(docs[i]["x"]);
double y = double.Parse(docs[i]["y"]);
mls.Add(new MyLocale(lname, y, x));
}
return mls;
}
}
}
http://ehpub.co.kr/kakaomap.html 내용은 다음과 같습니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Daum 지도 시작하기</title>
<style type="text/css">
html, body {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="map" style="width:95%;height:95%"></div>
<script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey=42dec13db4d8ba1a6a000f4691dc0056"></script>
<script>
var container = document.getElementById('map');
var options = {
center: new kakao.maps.LatLng(33.450701, 126.570667),
level: 3
};
var markerPosition = new kakao.maps.LatLng(33.450701, 126.570667);
var marker = new kakao.maps.Marker({ position: markerPosition });
var map = new kakao.maps.Map(container, options);
function setCenter(lat, lng) {
marker.setMap(null);
var mp= new kakao.maps.LatLng(lat,lng);
map.setCenter(mp);
marker = new kakao.maps.Marker({ position: mp});
marker.setMap(map);
}
</script>
</body>
</html>
반응형
'.NET > WPF' 카테고리의 다른 글
WPF에서 WebBrowser 컨트롤 스크립트 오류 없애기 (0) | 2020.07.31 |
---|---|
Grid 패널에 배치하기 – CS 코드로 작성하기 [언제나 WPF] (0) | 2020.07.08 |
Grid 패널에 배치하기 [언제나 WPF] (0) | 2020.05.28 |
[WPF] 시계 Gadget 만들기 (0) | 2020.04.25 |