I'm trying to use GraphHopper Route Optimization API for solving VRP with pickups and deliveries. I want to test it with an example from https://graphhopper.com/dashboard/#/editor. My request goes like this:
var vrp = {
"vehicles": [
{
"vehicle_id": "my_vehicle",
"start_address": {
"location_id": "berlin",
"lon": 13.406,
"lat": 52.537
}
}
],
"services": [
{
"id": "hamburg",
"name": "visit_hamburg",
"address": {
"location_id": "hamburg",
"lon": 9.999,
"lat": 53.552
}
},
{
"id": "munich",
"name": "visit_munich",
"address": {
"location_id": "munich",
"lon": 11.57,
"lat": 48.145
}
},
{
"id": "cologne",
"name": "visit_cologne",
"address": {
"location_id": "cologne",
"lon": 6.957,
"lat": 50.936
}
},
{
"id": "frankfurt",
"name": "visit_frankfurt",
"address": {
"location_id": "frankfurt",
"lon": 8.67,
"lat": 50.109
}
}
]
};
$.ajax({
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
url: 'https://graphhopper.com/api/1/vrp/optimize?key=[...]',
data: vrp,
dataType: "json",
success: function(json){
console.log(json);
}});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I get the following response:
screenshot
Waht may be the problem here?
I'have found solution in another so-question Send JSON data with jQuery.
Just use data: JSON.stringify(vrp)
Related
I wrote an api call in AngularJS to get data from backend JAVA
Here is my code
$scope.memberForm = {};
$scope.memberForm.member_address = {};
$scope.memberRegForm = function() {
$scope.btnloading = true;
$('#add-btn').prop('disabled', true);
url = '/member/add';
var request = $http({
method: "post",
url: url,
data: angular.toJson($scope.memberForm),
headers: {
'Content-Type': 'application/json'
},
});
}
But I am not getting desire response from backed, Response that's I am actually getting from backend is
{
"fullname":"sdfgsdf",
"fathername":"sdfsdf",
"email":"sdfsdf#gmail.com",
"date_of_birth":"1993-03-24",
"admission_date":"2018-10-22",
"phoneno":"0987654321",
"nationality":"United States",
"member_address":
{
"address":"5 meadow st",
"state":"Connecticut",
"zipcode":"06770",
"district":"zc"
},
"college":"cvbcv",
"qualification":"bcvbcv",
"slno_ivpr":"12"
}
But here is my desire response. I need member_address as an array of ojects instead of a single object
{
"fullname": "avinash",
"fathername": "xxxxxxx",
"date_of_birth": "1991-08-08",
"nationality": "indian",
"admission_date": "2015-08-08",
"college": "ABC college",
"slno_ivpr": null,
"phoneno": "1234567890",
"email": "avinash.mitresource#gmail.com",
"remarks": "no remarks",
"member_renewal": [],
"qualification": "MCA",
"member_address": [
{
"address": "piratlanka,repalle",
"state": "AP",
"district": "Guntur",
"zipcode": "522264",
"type": "PermanentAddress"
},
{
"address": "padamata",
"state": "AP",
"district": "vijayawada",
"zipcode": "522201",
"type": "ResidencialAddress"
}
]
}
I am getting json object from service, i wanted to iterate to this json object and fill array of class type.
Following is the code to call the service
public GetMapData(): Observable<Response> {
var path = 'http://my.blog.net/blog.php?type=trendingDestiny';
return this.http.get(path)
.map((response: Response) => {
if (response.status === 204) {
return undefined;
} else {
return response.json();
}
});
}
self.blogapi.GetMapData().subscribe(
x => {
this.MapData = x;
console.log("MapData", this.MapData);
});
Following is the json response
[
{
"post_id": 77,
"post_title": "Delhi",
"post_content": "DelhiDelhiDelhi",
"post_date": "2017-07-24 11:47:08",
"imageurl": false,
"cat_name": [
{
"term_id": 7,
"name": "FOODIE",
"slug": "foodie",
"category_parent": 0
}
],
"longitude": "75.857849",
"latitude": "33.888586",
"region_name": "Asia"
},
{
"post_id": 75,
"post_title": "Goa",
"post_content": "this is goa",
"post_date": "2017-07-24 11:03:59",
"imageurl": false,
"cat_name": [
{
"term_id": 7,
"name": "FOODIE",
"slug": "foodie",
"category_parent": 0
}
],
"longitude": "75.857849",
"latitude": "33.888586",
"region_name": "Asia"
}]
Following is the typescript code
this.MapData.forEach(map => {
this.Mapdatalist.push({
postid: map.post_id,
regionname: map.region_name,
longitude: map.longitude
});
});
or i also tried
for (let data of this.MapData)
{
console.log("error",data);
}
But nothing works. It gives me error
ERROR TypeError: Cannot read property 'forEach' of undefined
Please help what is going wrong.
var array= [
{
"post_id": 77,
"post_title": "Delhi",
"post_content": "DelhiDelhiDelhi",
"post_date": "2017-07-24 11:47:08",
"imageurl": false,
"cat_name": [
{
"term_id": 7,
"name": "FOODIE",
"slug": "foodie",
"category_parent": 0
}
],
"longitude": "75.857849",
"latitude": "33.888586",
"region_name": "Asia"
},
{
"post_id": 75,
"post_title": "Goa",
"post_content": "this is goa",
"post_date": "2017-07-24 11:03:59",
"imageurl": false,
"cat_name": [
{
"term_id": 7,
"name": "FOODIE",
"slug": "foodie",
"category_parent": 0
}
],
"longitude": "75.857849",
"latitude": "33.888586",
"region_name": "Asia"
}];
//console.log(array);
var mapdata = [];
$.each(array,function(i,v){
mapdata.push({"postid":v.post_id,"regionname":v.region_name,"longitude":v.longitude});
})
console.log(mapdata);
Please check that you assigned response to "this.MapData". I have created same scenario, where it is working. It might be case that "this.MapData" is not declared and defined correctly.
I am trying banging my head to filter json written below to get desired result.
[{
"Key": "EShSOKthupE=",
"ImageUrl": "path",
"Title": "ABC",
"CityId": 16,
"TimezoneShortName": "PYT",
"UtcHrsDiff": 8.5,
"PlaceKey": "QIZHdWOa77o=",
"PlaceName": "Shymala Hills Slums",
"Lat": 23.2424856,
"Long": 77.39488289999997,
"ActivityList": [ "Test Activity" ]
},
{
"Key": "NXLQpZAZT4M=",
"ImageUrl": "",
"Title": "ASAS",
"CityId": 17,
"TimezoneShortName": "AEST",
"UtcHrsDiff": 10,
"PlaceKey": "o4fAkahBzYY=",
"PlaceName": "ASAS",
"Lat": 12.9856503,
"Long": 77.60569269999996,
"ActivityList": [ "Adventure Sport" ]
}]
Now I want to get json like this from above json using lodash or undescore js.
[{
"PlaceKey": "QIZHdWOa77o=",
"PlaceName": "ABC",
"Lat": 23.2424856,
"Long": 77.39488289999997
},
{
"PlaceKey": "o4fAkahBzYY=",
"PlaceName": "ASAS",
"Lat": 12.9856503,
"Long": 77.60569269999996,
}]
Any help I can get on this?
Using lodash:
_.map(yourArray, (el => _.pick(el, ['PlaceKey', 'PlaceName', 'Lat', 'Long'])))
You can do this simply in Javascript, without the need of any external library like :
filteredArray=arr.map(function(item){
return {
"PlaceKey": item.PlaceKey,
"PlaceName": item.PlaceName,
"Lat": item.Lat,
"Long": item.Long,
}
})
use _.map function
var finalArray = _.map(your_arr, function(obj) {
return {
"PlaceKey": obj.PlaceKey,
"PlaceName": obj.PlaceName,
"Lat": obj.Lat,
"Long": obj.Long,
}
});
or just use the arrays map function:
var finalArray = your_arr.map(function(obj) {
return {
"PlaceKey": obj.PlaceKey,
"PlaceName": obj.PlaceName,
"Lat": obj.Lat,
"Long": obj.Long,
}
});
I'm getting this JSON file. The path arrays can be from 1 to 100,
{
"waypoints": [
{
"path0": [
{
"color": "#0000FF"
},
{
"lat": "37.9875000",
"lon": "23.7609260"
},
{
"lat": "37.9873130",
"lon": "23.7607460"
},
{
"lat": "37.9873840",
"lon": "23.7604100"
}
],
"path1": [
{
"color": "#00FF00"
},
{
"lat": "37.9873840",
"lon": "23.7604100"
},
{
"lat": "37.9878040",
"lon": "23.7605670"
},
{
"lat": "37.9882590",
"lon": "23.7607340"
}
],
"path2": [
{
"color": "#FF0000"
},
{
"lat": "37.9882590",
"lon": "23.7607340"
},
{
"lat": "37.9884690",
"lon": "23.7598760"
}
]
}
]
}
How can i see how many path(path0, path1, ..) arrays do i have inside waypoints in Javascript?
Using obj.waypoints.length returns 0.
Thank you.
On a modern browser you can use Object.keys :
var nbpaths = Object.keys(obj.waypoints[0]).length;
For compatibility with ie8, you can count like this :
var nbpaths = 0;
for (var key in obj.waypoints[0]) nbpaths++;
I'm trying to get json response from google maps using this code:
var address = 'Sydney,NSW';
$.ajax({
type: "GET",
url: "http://maps.googleapis.com/maps/api/geocode/json?sensor=false",
dataType: "jsonp",
data: ({"address":address}),
success: function(json){
if (json.status == "OK") {
alert(":)");
} else {
alert('wrong!');
}
}
});
It's working really good, but...
as a result i'm getting
Uncaught SyntaxError: Unexpected token :
in Chrome and
Error: no element found in FireFox..
it's seems that something wrong is with returned JSON, but it looking good:
{
"status": "OK",
"results": [ {
"types": [ "locality", "political" ],
"formatted_address": "Sydney New South Wales, Australia",
"address_components": [ {
"long_name": "Sydney",
"short_name": "Sydney",
"types": [ "locality", "political" ]
}, {
"long_name": "New South Wales",
"short_name": "New South Wales",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "Australia",
"short_name": "AU",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": -33.8689009,
"lng": 151.2070914
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": -34.1648540,
"lng": 150.6948538
},
"northeast": {
"lat": -33.5719182,
"lng": 151.7193290
}
},
"bounds": {
"southwest": {
"lat": -34.1692489,
"lng": 150.5022290
},
"northeast": {
"lat": -33.4245980,
"lng": 151.3426361
}
}
}
} ]
}
The return must be jsonp not json (the json should be wrapped into a function call)
Try using GM2 (looks like jsonp is not supported in GM3)
$.ajax({
type: "GET",
url: "http://maps.google.com/maps/geo?sensor=false&output=json&"+$.param({"q":address}),
dataType: "jsonp",
success: function(json){
if (json.Status.code == "200") {
alert(":)");
} else {
alert('wrong!');
}
}
});