Michael Smith
Michael Smith

Reputation: 498

Google Maps Embed: Turn iFrame into a link in iOS/Android

On my website I have a embedded Google map to the location of my business. When someone views my website on a iOS/Android device I would like them to be able to click the map and it open in the native Google Maps app, using the URL schema

comgooglemaps://

My plan is to turn the iFrame into a link on those devices but I am having no luck.

Anyone have any idea how to do this or have a better idea of how to implement this functionality.

Upvotes: 0

Views: 2529

Answers (2)

Rashid Ali
Rashid Ali

Reputation: 569

I have JSON

"GoogleMap":"\u003ciframe src=\"https://www.google.com/maps/embed?pb=!1m16!1m12!1m3!1d434549.40374533384!2d74.24349628287739!3d31.690830957117996!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!2m1!1sPakistan+Lahore!5e0!3m2!1sen!2s!4v1395138949280\" width=\"600\" height=\"450\" frameborder=\"0\" style=\"border:0\"\u003e\u003c/iframe\u003e",

When parse it to String, .... Result

String iframe = "https://www.google.com/maps/embed?pb=!1m16!1m12!1m3!1d434549.40374533384!2d74.24349628287739!3d31.690830957117996!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!2m1!1sPakistan+Lahore!5e0!3m2!1sen!2s!4v1395138949280 width=600 height=450 frameborder=0 style=border:0";

layout: activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#00FF00" >

        <TextView
            android:id="@+id/header_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Google Map"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />
    </RelativeLayout>

    <WebView
        android:id="@+id/googlemap_webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp" />

</LinearLayout>

Java Class: MainActivity

public class MainActivity extends Activity {

    private WebView googleMapWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeUI();
    }

    /**
     * This method is used to Initialize UI components
     */
    private void initializeUI() {

            String iframe = "<iframe src=https://www.google.com/maps/embed?pb=!1m16!1m12!1m3!1d434549.40374533384!2d74.24349628287739!3d31.690830957117996!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!2m1!1sPakistan+Lahore!5e0!3m2!1sen!2s!4v1395138949280 width=600 height=450 frameborder=0 style=border:0</iframe>";
            googleMapWebView = (WebView) findViewById(R.id.googlemap_webView);
            googleMapWebView.getSettings().setJavaScriptEnabled(true);
            googleMapWebView.loadData(iframe, "text/html", "utf-8");
    }

}

AndroidManifest.xml

  1. <uses-permission android:name="android.permission.INTERNET"/>

Also chek WIFI permission etc ..

Upvotes: 3

Michael Smith
Michael Smith

Reputation: 498

I decided to chance my approach, when the browser is below a certian size I use jQuery to hide the iFrame and replace it with a static image. I was then a simple matter.

Upvotes: 0

Related Questions