הוספת מפה מעוצבת

בחירת פלטפורמה: Android iOS JavaScript

במדריך הזה תלמדו איך להוסיף ל-Android מפה עם עיצוב מותאם אישית אפליקציה. המדריך משתמש במצב לילה כדוגמה לעיצוב בהתאמה אישית.

בעזרת אפשרויות הסגנון אפשר להתאים אישית את התצוגה של מפת Google הרגילה סגנונות, שינוי התצוגה החזותית של תכונות כמו כבישים, פארקים, עסקים, ונקודות עניין אחרות. כלומר, אתם יכולים להדגיש רכיבים מסוימים במפה או להפוך את המפה להשלמה לסגנון של אפליקציה.

אפשר לעצב רק את סוג המפה normal. העיצוב לא משפיע מפות פנים.

קבל את הקוד

שכפול או הורדה של מאגר הדוגמאות ל-Android API גרסה 2 של מפות Google מ-GitHub.

הגדרת פרויקט הפיתוח

פועלים לפי השלבים הבאים כדי ליצור את הפרויקט במדריך ב-Android Studio.

  1. מורידים ומתקינים את Android Studio.
  2. מוסיפים את החבילה Google Play Services ל-Android Studio.
  3. שכפול או הורדה של מאגר הדוגמאות ל-Android API גרסה 2 של מפות Google אם לא עשיתם זאת כשהתחלתם לקרוא את המדריך הזה.
  4. מייבאים את פרויקט המדריך:

    • ב-Android Studio, בוחרים באפשרות File > חדש > ייבוא פרויקט.
    • עוברים למיקום שבו שמרתם את המאגר של דוגמאות ל-Google Maps Android API v2 אחרי ההורדה.
    • מוצאים את הפרויקט StyledMap במיקום הזה:
      PATH-TO-SAVED-REPO/android-samples/tutorials/StyledMap
    • בוחרים את ספריית הפרויקט ולוחצים על אישור. עכשיו Android Studio יוצרת את הפרויקט באמצעות הכלי ל-build של Gradle.

קבלת מפתח API והפעלת ממשקי ה-API הנדרשים

כדי להשלים את המדריך הזה, צריך מפתח API של Google עם הרשאה לשימוש ב-Maps SDK ל-Android.

כדי לקבל מפתח ולהפעיל את ה-API, צריך ללחוץ על הלחצן שלמטה.

לקבלת מפתח

פרטים נוספים זמינים במדריך קבלת מפתח API.

הוספה של מפתח ה-API לאפליקציה

  1. עורכים את הקובץ gradle.properties של הפרויקט.
  2. מדביקים את מפתח ה-API בערך של המאפיין GOOGLE_MAPS_API_KEY. כשמפתחים את האפליקציה, Gradle מעתיק את מפתח ה-API למניפסט Android של האפליקציה.

    GOOGLE_MAPS_API_KEY=PASTE-YOUR-API-KEY-HERE
    

פיתוח והרצה של האפליקציה

  1. מחברים מכשיר Android למחשב. פועלים לפי הוראות להפעלת אפשרויות למפתחים ב-Android את המכשיר ולהגדיר את המערכת לזהות אותו. (לחלופין, אפשר להשתמש ניהול מכשיר וירטואלי (AVD) של Android כדי להגדיר מכשיר וירטואלי במכשיר. כאשר בוחרים אמולטור, חשוב לבחור תמונה שכוללת ממשקי Google API. פרטים נוספים זמינים במדריך למתחילים).
  2. ב-Android Studio, לוחצים על אפשרות התפריט הפעלה (או על סמל לחצן ההפעלה). בוחרים מכשיר בהתאם להנחיה.

Android Studio מפעיל את Gradle כדי ליצור את האפליקציה, ואז מריץ אותה במכשיר או באמולטור. אמורה להופיע מפה עם תמונה חשוכה (מצב לילה) בסגנון דומה לתמונה בדף הזה.

פתרון בעיות:

הבנת הקוד

בקטע הזה במדריך מוסבר על החלקים המשמעותיים ביותר באפליקציה StyledMap, כדי לעזור לכם להבין איך ליצור אפליקציה דומה.

הוספת משאב שמכיל אובייקט בסגנון JSON

הוספת משאב לפרויקט הפיתוח, המכיל את הצהרות הסגנון שלך בפורמט JSON. אפשר להשתמש במשאב גולמי או במחרוזת, כפי שמוצג בדוגמאות שלמטה.

משאב גולמי

מגדירים משאב גלם ב-/res/raw/style_json.json, שמכיל את הצהרת הסגנון ב-JSON של עיצוב מצב לילה:

משאב מחרוזת

מגדירים משאב מחרוזת ב-/res/values/style_strings.xml, שמכיל את הצהרת הסגנון ב-JSON לסגנון של מצב לילה. הזה המדריך משתמש בשם המחרוזת style_json. בקובץ הזה צריך להשתמש בלוכסן הפוך כדי לסמן בתו בריחה (escape) את המירכאות:

העברת אובייקט בסגנון JSON למפה

כדי לעצב את המפה, שולחים אל GoogleMap.setMapStyle() אובייקט MapStyleOptions שמכיל את הסגנון שלכם בפורמט JSON.

משאב גולמי

בדוגמת הקוד הבאה, הפרויקט מכיל משאב גולמי בשם style_json:

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.example.styledmap;

import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;

/**
 * A styled map using JSON styles from a raw resource.
 */
public class MapsActivityRaw extends AppCompatActivity
        implements OnMapReadyCallback {

    private static final String TAG = MapsActivityRaw.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps_raw);

        // Get the SupportMapFragment and register for the callback
        // when the map is ready for use.
        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map when it's available.
     * The API invokes this callback when the map is ready for use.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {

        try {
            // Customise the styling of the base map using a JSON object defined
            // in a raw resource file.
            boolean success = googleMap.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(
                            this, R.raw.style_json));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }
        // Position the map's camera near Sydney, Australia.
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-34, 151)));
    }
}

הפריסה (activity_maps_raw.xml) נראית כך:

משאב מחרוזות

בדוגמת הקוד הבאה, הפרויקט מכיל משאב מחרוזת בשם style_json:

package com.example.styledmap;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;

/**
 * A styled map using JSON styles from a string resource.
 */
public class MapsActivityString extends AppCompatActivity
        implements OnMapReadyCallback {

    private static final String TAG = MapsActivityString.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps_string);

        // Get the SupportMapFragment and register for the callback
        // when the map is ready for use.
        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map when it's available.
     * The API invokes this callback when the map is ready for use.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {

        // Customise the styling of the base map using a JSON object defined
        // in a string resource file. First create a MapStyleOptions object
        // from the JSON styles string, then pass this to the setMapStyle
        // method of the GoogleMap object.
        boolean success = googleMap.setMapStyle(new MapStyleOptions(getResources()
                .getString(R.string.style_json)));

        if (!success) {
            Log.e(TAG, "Style parsing failed.");
        }
        // Position the map's camera near Sydney, Australia.
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-34, 151)));
    }
}

הפריסה (activity_maps_string.xml) נראית כך:

מידע נוסף על הצהרות בסגנון JSON

במפות עם עיצוב נעשה שימוש בשני מושגים כדי להחיל צבעים ושינויים אחרים בסגנון על המפה:

  • בוררים מציינים את הרכיבים הגיאוגרפיים שאפשר לשנות את הסגנון שלהם במפה. אלה כוללים כבישים, פארקים, גופי מים ועוד, וגם את התוויות שלהם. הבוררים כוללים תכונות ורכיבים, שמצוינים כנכסי featureType ו-elementType.
  • סטיילרים הם מאפייני צבע וחשיפה שאפשר ליצור להחיל על רכיבי מפה. הם מגדירים את הצבע המוצג באמצעות שילוב של ערכי גוון, צבע ובהירות/גמא.

תיאור מפורט של אפשרויות העיצוב של JSON זמין בחומר העזרה בנושא עיצוב.

אשף העיצוב בפלטפורמה של מפות Google

אפשר להשתמש באשף עיצוב הפלטפורמה של מפות Google כדי ליצור במהירות אובייקט של עיצוב JSON. ה-SDK של מפות Google ל-Android תומך באותו סגנון הצהרות כ-Maps JavaScript API.

השלב הבא

בקישור הבא מוסבר איך להסתיר תכונות במפה באמצעות עיצוב.