Hello, I have a xamairn forms application that searches for schools on a map, so far I have my map and a pin that I put manually, the point is that I use Google Maps places to find other places and I try to put them on the map but I don't get it, I have a suspicion that it could be because I'm using a foreach and well the same variable for all, does anyone know how I can render several pins that I call from this api?
This is what I have
public partial class Map: ContentPage {
public Map() {
InitializeComponent();
Look4Schools();
}
protected override async void OnAppearing() {
base.OnAppearing();
InitializeComponent();
if (!CrossConnectivity.Current.IsConnected) {
await DisplayAlert("Advertencia ! ", "PORFAVOR ACITVE SUS DATOS PARA CONTINUAR ", "Ok");
}
var pos = CrossGeolocator.Current.GetPositionAsync();
var lon = pos.Result.Latitude;
var lat = pos.Result.Longitude;
MyMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(lon, lat),
Distance.FromKilometers(8)));
var pin = new Pin {
Type = PinType.Place,
Position = new Position(lon, lat),
Label = "Mi ubicacion",
Address = "aqui se encuentra usted",
};
pin.Clicked += async(sender, e) => {
await DisplayAlert(pin.Label, "" + pin.Address, "Cancel");
};
MyMap.Pins.Add(pin);
}
public async void Look4Schools() {
var pos = CrossGeolocator.Current.GetPositionAsync();
var lon = pos.Result.Latitude;
var lat = pos.Result.Longitude;
HttpClient client = new HttpClient();
var response = await client.GetAsync("https://maps.googleapis.com/maps/api/place/textsearch/json?query=schools&location=" + lon + "," + lat + "&radius=10000&key=MIAPIKEY");
switch (response.StatusCode) {
case (System.Net.HttpStatusCode.OK):
string xjson = await response.Content.ReadAsStringAsync();
var json_ = JsonConvert.DeserializeObject < GMapsApiCall > (xjson);
var x = json_;
var xc = x.results.Count;
if (xc == 0) {
await DisplayAlert("Advertencia ! ", "No hay escuelas cerca", "Ok");
} else {
await DisplayAlert("Exito ! ", "Encontramos " + xc + " escuelas cerca", "Ok");
foreach(Result r in x.results) {
Pin pinx = new Pin {
Type = PinType.Place,
Position = new Position(r.geometry.location.lng, r.geometry.location.lat),
Label = r.name,
Address = r.formatted_address,
};
pinx.Clicked += async(sender, e) => {
await DisplayAlert(pinx.Label, "The address is: " + pinx.Address, "Cancel");
};
MyMap.Pins.Add(pinx);
}
}
break;
default:
await DisplayAlert("Advertencia ! ", "Error al cargar el mapa ", "Ok");
break;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
x:Class="App1.Map">
<maps:Map x:Name="MyMap" IsShowingUser="True">
<x:Arguments>
<maps:MapSpan>
<x:Arguments>
<maps:Position>
<x:Arguments>
<x:Double>23.6260333 </x:Double>
<x:Double>-102.0194722</x:Double>
</x:Arguments>
</maps:Position>
<x:Double>0.01</x:Double>
<x:Double>0.01</x:Double>
</x:Arguments>
</maps:MapSpan>
</x:Arguments>
<maps:Map.Pins>
</maps:Map.Pins>
</maps:Map>
</ContentPage>
This is my model of the query I do
using System;
using System.Collections.Generic;
using System.Text;
namespace App1.Models_
{
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public Viewport viewport { get; set; }
}
public class PlusCode
{
public string compound_code { get; set; }
public string global_code { get; set; }
}
public class OpeningHours
{
public bool open_now { get; set; }
}
public class Photo
{
public int height { get; set; }
public List<string> html_attributions { get; set; }
public string photo_reference { get; set; }
public int width { get; set; }
}
public class Result
{
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string icon { get; set; }
public string id { get; set; }
public string name { get; set; }
public string place_id { get; set; }
public PlusCode plus_code { get; set; }
public double rating { get; set; }
public string reference { get; set; }
public List<string> types { get; set; }
public int user_ratings_total { get; set; }
public OpeningHours opening_hours { get; set; }
public List<Photo> photos { get; set; }
}
public class GMapsApiCall
{
public List<object> html_attributions { get; set; }
public string next_page_token { get; set; }
public List<Result> results { get; set; }
public string status { get; set; }
}
}
And this is what it looks like
Here I leave some of my references
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/map/pins
It's already in case it works for someone, just change the latitude and longitude of the place
and boom ready