Imran Mozumder
Imran Mozumder

Reputation: 268

how to add google map in windows phone 8 app

I am developing an app which uses Map service for Windows Phone 8.

I followed a link http://themightyhedgehog.blogspot.de/2013/01/how-to-use-google-maps-in-your-own.html and tries to develop an app containing Google Map.

But I got an error in Xaml on line MapAppScope:BindingHelpers.TileSource="{Binding GoogleMap}"

and the error I got are:

  1. BuildingHelpers is not supported in Silverlight.
  2. The attachable property 'TileSource' was not found in type 'BuildingHelpers'.
  3. The Namespace prefix "MapAppScope" is not defined.

In Xaml:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Microsoft_Phone_Controls_Maps:Map 
Name="MyMap" 
Grid.Column="1" 
LogoVisibility="Collapsed" 
d:LayoutOverrides="GridBox" 
           MapAppScope:BindingHelpers.TileSource="{Binding GoogleMap}"
            Margin="0,0,0,2">
            <Microsoft_Phone_Controls_Maps:Map.Mode>
                <MSPCMCore:MercatorMode/>
            </Microsoft_Phone_Controls_Maps:Map.Mode>
        </Microsoft_Phone_Controls_Maps:Map>


    </Grid>

In .CS:

    namespace Google_Map_App
{
    public enum GoogleType
    {
        Street = 'm',
        Hybrid = 'y',
        Satellite = 's',
        Physical = 't',
        PhysicalHybrid = 'p',
        StreetOverlay = 'h',
        WaterOverlay = 'r'
    }

    public class Google : TileSource
    {
        public Google()
        {
            MapType = GoogleType.Street;
            UriFormat = @"http://mt{0}.google.com/vt/lyrs={1}&z={2}&x={3}&y={4}";
        }

        public GoogleType MapType { get; set; }

        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            return new Uri(
              string.Format(UriFormat, (x % 2) + (2 * (y % 2)),
              (char)MapType, zoomLevel, x, y));
        }
    }
    public static class BindingHelpers
    {
        //Used for binding a single TileSource object to a Bing Maps control
        #region TileSourceProperty

        // Name, Property type, type of object that hosts the property, method to call when anything changes
        public static readonly DependencyProperty TileSourceProperty =
            DependencyProperty.RegisterAttached("TileSource", typeof(TileSource),
            typeof(BindingHelpers), new PropertyMetadata(SetTileSourceCallback));

        // Called when TileSource is retrieved
        public static TileSource GetTileSource(DependencyObject obj)
        {
            return obj.GetValue(TileSourceProperty) as TileSource;
        }

        // Called when TileSource is set
        public static void SetTileSource(DependencyObject obj, TileSource value)
        {
            obj.SetValue(TileSourceProperty, value);
        }

        //Called when TileSource is set
        private static void SetTileSourceCallback(object sender, DependencyPropertyChangedEventArgs args)
        {

            var map = sender as Map;
            var newSource = args.NewValue as TileSource;
            if (newSource == null || map == null) return;

            // Remove existing layer(s)
            for (var i = map.Children.Count - 1; i >= 0; i--)
            {
                var tileLayer = map.Children[i] as MapTileLayer;
                if (tileLayer != null)
                {
                    map.Children.RemoveAt(i);
                }
            }

            var newLayer = new MapTileLayer();
            newLayer.TileSources.Add(newSource);
            map.Children.Add(newLayer);
        }

        #endregion
    }
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }


    }
}

Upvotes: 5

Views: 4436

Answers (1)

rbrundritt
rbrundritt

Reputation: 17954

Note that using Google Maps tiles in the Bing Maps WP8 or Windows 8 control is against the terms of use of both the Bing Maps and Google Maps API's. Any app found in the app store doing this will be removed.

Upvotes: 1

Related Questions