How do you show multiple locations on a map with .NET MAUI? - .net-maui

I have been able to open the default map application and display a single location:
var location = new Location(latitude, longitude);
var options = new MapLaunchOptions { Name = locationName };
try
{
await Map.Default.OpenAsync(location, options);
}
catch (Exception ex)
{
// No map application available to open
}
Wondering if the ability to open with multiple locations pinned exists?

You will first need to load in the location data, whether that is from an embedded resource or API call. Once you have the lat/lng data pulled in (mine is in a List) you can go ahead and add them. You will need map.Layers.Add(CreatePointLayer()); to call the code below:
private MemoryLayer CreatePointLayer()
{
return new MemoryLayer
{
Name = "Points",
IsMapInfoLayer = true,
Features = GetLocsFromList(),
Style = SymbolStyles.CreatePinStyle()
};
}
private IEnumerable<IFeature> GetLocsFromList()
{
var locs = Locs; //<- Locs is the List<Locations>
return locs.Select(l => {
var feature = new PointFeature(SphericalMercator.FromLonLat(l.lng, l.lat).ToMPoint());
return feature;
});
}

Related

Get polygon from Azure Maps Search

I'm trying to use Azure.Maps.Search to give me a polygon for a result. For example, if I search for "Birmingham" I would like a result for that municipality with a collection of geopoints defining the boundary.
Is this possible?
var credential = new AzureKeyCredential("............");
var client = new MapsSearchClient(credential);
Response<SearchAddressResult> searchResult = await client.SearchAddressAsync(
query: "Birmingham",
options: new SearchAddressOptions
{
ExtendedPostalCodesFor=new SearchIndex[] { SearchIndex.PointAddresses },
CountryFilter = new string[] { "GB" },
Top = 1,
EntityType = GeographicEntity.Municipality
});
Yes, this is possible. The search address API will contain a DataSources.Geometries.ID value in the results that is the ID of the unique boundary for that result. You can take this ID and pass it into the GetPolygonsAsync API in the Azure.Maps.Search Nuget package.
using Azure;
using Azure.Maps.Search;
using Azure.Maps.Search.Models;
namespace AzureMapsTest
{
internal class Program
{
private const string MapsApiKey = "...........";
static async Task Main(string[] args)
{
var credential = new AzureKeyCredential(MapsApiKey);
var client = new MapsSearchClient(credential);
SearchAddressOptions singleSearchResultOptions = new SearchAddressOptions { Top = 1 };
Response<SearchAddressResult> searchResult =
await client.SearchAddressAsync(
"Ealing, London, England",
singleSearchResultOptions);
Response<PolygonResult> polygons =
await client.GetPolygonsAsync(new string[] { searchResult.Value.Results[0].DataSources.Geometry.Id });
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(polygons.Value.Polygons));
}
}
}

Await for ChangeResourceRecordSetsAsync hangs forever when trying to create Route53 DNS record

I am trying to create a TXT record in AWS Route53, but while awaiting the call, it will never complete.
The record is not being created in the hosted zone. The account has the correct permissions which I tested using the AWS CLI.
I am able to list hosted zones as also shown in the code below.
public static async Task CreateRecordAsync()
{
var route53Client = new AmazonRoute53Client("<myAccessKey>", "<mySecretKey>", RegionEndpoint.EUCentral1);
var test = new ListHostedZonesByNameRequest();
var testResponse = route53Client.ListHostedZonesByNameAsync(test);
foreach (var zone in testResponse.Result.HostedZones)
{
Console.WriteLine($"{zone.Name}{zone.Id}");
}
var response = await route53Client.ChangeResourceRecordSetsAsync(new ChangeResourceRecordSetsRequest
{
ChangeBatch = new ChangeBatch
{
Changes = new List<Change> {
new Change {
Action = "CREATE",
ResourceRecordSet = new ResourceRecordSet {
Name = "my.domain.net",
Type = "TXT",
TTL = 60,
ResourceRecords = new List<ResourceRecord>
{
new ResourceRecord
{
Value = "test txt value"
}
}
}
}
},
Comment = "Test Entry"
},
HostedZoneId = "Z2Q***********"
});
}
The problem was that the TXT record had to be enclosed in double quotes. I just had to catch the correct exception.
try
{
....
ChangeResourceRecordSetsResponse recordsetResponse =
await route53Client.ChangeResourceRecordSetsAsync(recordsetRequest);
....
}
catch (InvalidChangeBatchException ex)
{
Console.WriteLine(ex);
}
Which means that I should have constructed my record like this using.
ResourceRecordSet recordSet = new ResourceRecordSet
{
Name = chosenDomain,
TTL = 60,
Type = RRType.TXT,
ResourceRecords = new List<ResourceRecord>
{
new ResourceRecord
{
Value = "\"This is my test txt record\""
}
}
};

Storage quota has been exceeded for this service. You must either delete documents first, or use a higher SKU for additional quota

In azure search getting error sometimes not every time when I'm create or update index in azure.
and I'm tying post multiple data as per guideline How to index large data sets in Azure Search
is there any way to update existing data in azure? How to rebuild an index
Some azure Available tiers Available tiers
Code this simple example
private static SearchServiceClient searchServiceClient;
public virtual void CreateSearchServiceClient()
{
searchServiceClient = new SearchServiceClient(searchServiceName, new SearchCredentials("APIKEY"));
}
public virtual Index CreateIndex()
{
CreateSearchServiceClient();
Index index = new Index();
string scoringProfile = "AzureSearchScoringProfile";
// Create index if search client service is available
if (searchServiceClient != null)
{
AzureSearchItem azureSearchItem = new AzureSearchItem();
// Map index schema
var definition = new Index()
{
Name = IndexName,
Fields = FieldBuilder.BuildForType<AzureSearchItem>(),
Suggesters = new List<Suggester>() {
new Suggester()
{
Name = ConstantKeys.AzureSearchTopBarSuggestor,
SourceFields = FormatSuggesterFields()
}
}
};
if (!string.IsNullOrEmpty(scoringProfile))
{
definition.ScoringProfiles = new List<ScoringProfile>()
{
new ScoringProfile()
{
Name = scoringProfile
}
};
}
definition = SetAnalyzer(definition);
// Create index
index = searchServiceClient.Indexes.CreateOrUpdate(definition);
}
return index;
}

Microsoft.Azure.CognitiveServices.Search.EntitySearch.EntitySearchClient.Entities.Search(location: <latitude>:? <longitude>:?

I'm running the Microsoft sample for Bing Entity Search using the SDK and (NOT the REST API.) I can.t figure out how to enter the location key/value pair, based on this documentation:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.cognitiveservices.search.entitysearch.entitiesoperationsextensions.search?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(Microsoft.Azure.CognitiveServices.Search.EntitySearch.EntitiesOperationsExtensions.Search);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.7.2);k(DevLang-csharp)%26rd%3Dtrue&view=azure-dotnet
I would like to continue to use named parameters so I can still do this:
var restaurants = client.Entities.Search(query: currentQuery, location: lat:? long:?);
The method with the query and location from the Microsoft sample will look like this:
public static void MultipleCurrentQueryLookup(string subscriptionKey)
{
var client = new EntitySearchClient(new ApiKeyServiceClientCredentials subscriptionKey));
try
{
string currentQuery = Settings1.Default.CurrentQuery;
var restaurants = client.Entities.Search(query: currentQuery,
location: "lat:47.623, long:-122.361, re:380m");
if (restaurants?.Places?.Value?.Count > 0)
{
// get all the list items that relate to this query
var listItems = restaurants.Places.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == EntityScenario.ListItem).ToList();
if (listItems?.Count > 0)
{
var sb = new StringBuilder();
foreach (var item in listItems)
{
var place = item as Place;
if (place == null)
{
Console.WriteLine("Unexpectedly found something that isn't a place named \"{0}\"", item.Name);
continue;
}
sb.AppendFormat(",{0} ({1}) {2}", place.Name, place.Telephone, place.Url);
}
Console.WriteLine("Ok, we found these places: ");
Console.WriteLine(sb.ToString().Substring(1));
}
else
{
Console.WriteLine("Couldn't find any relevant results for \"The Current Query\"");
}
}
else
{
Console.WriteLine("Didn't see any data..");
}
}
catch (ErrorResponseException ex)
{
Console.WriteLine("Encountered exception. " + ex.Message);
}
}

Node + ES6 classes: Setting up a set of cached objects

I've tried to search for instance caching and singletons on Google and StackOverflow without success, seeing only posts about module.exports, if you know a post that answers this question, feel free to reference it. Thank you!
I have an application that needs to work on a set of objects that rarely change, and hence need to be cached for performance optimisation.
Here is a toy example where a single property is set directly.
When I call the application, I export an object that will contain the set of cached objects in assets_cached.js:
const Assets = {};
module.exports.Assets = Assets;
In another module of the application I have an ES6 class:
const _ = require('lodash')
const { Assets } = require('./assets_cached')
class Asset {
constructor(id, some_property) {
if (id in Assets) {
// Update instance data with cached properties
_.assign(this, Assets_cached[id]);
} else {
// If it's not cached, create a new object
this.id = id;
this.some_property = some_property;
// Cache this object
Assets_cached[id] = this;
}
}
getProperty() {
return this.some_property;
}
setProperty(value) {
this.some_property = value;
// Is there a way of avoiding having to do this double assignment?
Assets_cached[id].some_property = value;
}
}
module.exports = Asset;
How may I avoid having to set the some_property twice (in the current instance and the cache, while ensuring that other instances are updated in parallel)?
Ideally I'd like to do something like:
if (id in Assets) {
this = Assets.cached[id]
}
inside the constructor, but this is not possible.
What's the most elegant and correct way of making this work?
Ideally I'd like to do something like this = Assets.cached[id] inside the constructor
The magic keyword here is return. You can just return an arbitrary object from the constructor and it will be used instead of this.
constructor(id, some_property) {
if (id in Assets) {
// use cached instance instead of creating a new one
return Assets_cached[id];
} else {
this.id = id;
this.some_property = some_property;
// Cache this object
Assets_cached[id] = this;
}
}
Here is the approach to the comment that was made some half an hour ago ...
const { Assets_cached } = require('./assets_cached');
// const { AssetStore } = require('./assetstore');
class Asset {
constructor(id, some_property) { // clean/lean constructor.
this.id = id;
this.some_property = some_property;
}
getProperty() {
return this.some_property;
}
setProperty(value) {
this.some_property = value;
}
}
function isAsset(type) {
// poor man's approach ... change to something more feasible.
return (type instanceof Asset);
}
function createAsset(id, some_property) { // factory that also handles caching.
var
asset = Assets_cached[id];
// asset = AssetStore.get(id);
if (!(asset && isAsset(asset))) {
asset = Assets_cached[id] = (new Asset(id, some_property));
// AssetStore.put(id, (asset = new Asset(id, some_property)));
}
return asset;
}
module.exports = {
create : createAsset,
isAsset : isAsset
};
Note
One also should consider providing a minimal API to Assets_cached, something like put/set, get and delete instead of Assets_cached being an entirely exposed, plain key-value store.

Resources