Showing posts with label Google Maps JavaScript API v3. Show all posts
Showing posts with label Google Maps JavaScript API v3. Show all posts

WebView to load Google Maps JavaScript API v3 with Panoramio Layer

This example load WebView with built-in HTML inside APK, using Google Maps JavaScript API v3 with Panoramio Layer.

WebView to load Google Maps JavaScript API v3 with Panoramio Layer

The code created using project wizard of new Android SDK Tools 22.6, MainActivity extends android.support.v7.app.ActionBarActivity with Fragment.

Create /assets/panoramiomap.html, copy the JavaScript + HTML example from Google Maps JavaScript API v3 example with Panoramio Layer.
<!DOCTYPE html>
<html>
<head>
<title>Panoramio Layer</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
#photo-panel {
background: #fff;
padding: 5px;
overflow-y: auto;
overflow-x: hidden;
width: 300px;
max-height: 300px;
font-size: 14px;
font-family: Arial;
border: 1px solid #ccc;
box-shadow: -2px 2px 2px rgba(33, 33, 33, 0.4);
display: none;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=panoramio"></script>
<script>
function initialize() {
var mapOptions = {
zoom: 16,
center: new google.maps.LatLng(47.651743, -122.349243)
};

var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

var panoramioLayer = new google.maps.panoramio.PanoramioLayer();
panoramioLayer.setMap(map);

var photoPanel = document.getElementById('photo-panel');
map.controls[google.maps.ControlPosition.RIGHT_TOP].push(photoPanel);

google.maps.event.addListener(panoramioLayer, 'click', function(photo) {
var li = document.createElement('li');
var link = document.createElement('a');
link.innerHTML = photo.featureDetails.title + ': ' +
photo.featureDetails.author;
link.setAttribute('href', photo.featureDetails.url);
li.appendChild(link);
photoPanel.appendChild(li);
photoPanel.style.display = 'block';
});
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>
<body>
<ul id="photo-panel">
<li><strong>Photos clicked</strong></li>
</ul>
<div id="map-canvas"></div>
</body>
</html>

/res/layout/activity_main.xml
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://arteluzevida.blogspot.com/"
android:textStyle="bold" />

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.androidwebviewpanoramio.MainActivity"
tools:ignore="MergeRootFrame"
android:background="#505050" />

</LinearLayout>

Modify /res/layout/fragment_main.xml to add a <WebView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidwebviewpanoramio.MainActivity$PlaceholderFragment" >

<WebView
android:id="@+id/mybrowser"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

MainActivity.java
package com.example.androidwebviewpanoramio;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class MainActivity extends ActionBarActivity {

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

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

WebView myBrowser;

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);

myBrowser = (WebView) rootView.findViewById(R.id.mybrowser);
myBrowser.loadUrl("file:///android_asset/panoramiomap.html");
myBrowser.getSettings().setJavaScriptEnabled(true);

return rootView;
}
}

}

<uses-permission android:name="android.permission.INTERNET"/> is needed in AndroidManifest.xml.

download filesDownload the files.

Download and try the APK.


)



Embed html using Google Maps JavaScript API v3 in Android App

The Google Maps Javascript API Version 3 is now the official Javascript API. Version 2 of this API has been officially deprecated as per our deprecation policy.

The Google Maps Javascript API lets you embed Google Maps in your own web pages. Version 3 of this API is especially designed to be faster and more applicable to mobile devices, as well as traditional desktop browser applications.

The API provides a number of utilities for manipulating maps (just like on the http://maps.google.com web page) and adding content to the map through a variety of services, allowing you to create robust maps applications on your website.

The JavaScript Maps API V3 is a free service, available for any web site that is free to consumers. Please see the terms of use for more information.




It's easy to embed WebView, load with HTML using Google Maps JavaScript API v3, in Android App:



Create /assets/simplemap.html, copy from Simple Map example in Google Maps JavaScript API v3, html code with Google Maps JavaScript API v3.
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>


Modify /res/layout/activity_main.xml, to add <WebView> in layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://arteluzevida.blogspot.com/"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:autoLink="web" />
<WebView
android:id="@+id/mybrowser"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>


MainActivity.java
package com.example.androidsimplemap;

import android.os.Bundle;
import android.app.Activity;
import android.webkit.WebView;

public class MainActivity extends Activity {

WebView myBrowser;

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

myBrowser = (WebView)findViewById(R.id.mybrowser);
myBrowser.loadUrl("file:///android_asset/simplemap.html");
myBrowser.getSettings().setJavaScriptEnabled(true);
}

}


Note that, in order for your Activity to access the Internet and load web pages in a WebView, you must add the INTERNET permissions to your Android Manifest file:

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



add@2014-05-22:

download filesDownload the files.

Download and try the APK.